This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 181068 - The type [null] is not the expected [EntityType] for the key class [class myClass]
Summary: The type [null] is not the expected [EntityType] for the key class [class myC...
Status: RESOLVED INVALID
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 6.x
Hardware: PC Linux
: P3 normal (vote)
Assignee: Sergey Petrov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-02-23 01:15 UTC by roti
Modified: 2011-11-16 16:40 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
sample with the sample database (13.18 KB, application/bzip2)
2010-02-24 15:47 UTC, roti
Details

Note You need to log in before you can comment on or make changes to this bug.
Description roti 2010-02-23 01:15:55 UTC
1. new web project
2. create entity classes from database
3. create jpa controller classes form entity classes
4. insert code to jsf page, "jsf data from entity"
5. run project with gf3, and get: The type [null] is not the expected [EntityType] for the key class [class myClass]

tested with eclipselink 2.0.1 too

java.lang.IllegalArgumentException: The type [null] is not the expected [EntityType] for the key class [class entity.MyClass].
	at org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.entity(MetamodelImpl.java:152)
	at org.eclipse.persistence.internal.jpa.querydef.AbstractQueryImpl.from(AbstractQueryImpl.java:97)
	at controller.MyClassJpaController.findMyClassEntities(MyClassJpaController.java:104)
...


see also:
http://forums.sun.com/thread.jspa?threadID=5423288
Comment 1 Sergey Petrov 2010-02-23 01:29:24 UTC
it may be a problem in el2.01, from stacktrace it can't recognize entity class, can you provide your persistence.xml?
it may be good to attach your sample project to the issue.
Comment 2 Sergey Petrov 2010-02-23 01:31:20 UTC
or may be it's something related to abstract controller introduction.
Comment 3 roti 2010-02-23 02:14:06 UTC
the persistence.xml contains nothing unusual:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="myProjPU" transaction-type="JTA">
    <jta-data-source>myProj</jta-data-source>
    <properties/>
  </persistence-unit>
</persistence>


the generated controller class snipet:
...
private List<VirSablonok> findMyClassEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(MyClass.class)); // line 104
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
Comment 4 Sergey Petrov 2010-02-24 04:36:41 UTC
Can you attach sample project or provide all steps as I'm not experienced in Spring?
Now I se it's more like project configuration problem, may be jpa controller is unmanaged and need to be added to some configuration file (in case of jsf faces-config, in case of spring I don't know is there any similar config).
Also it may be good to use session bean controller instead of plain class jpa controller.
Comment 5 Sergey Petrov 2010-02-24 06:07:16 UTC
as example, in case of jsf, there should be 
    <managed-bean>
        <managed-bean-name>discountCodeJpa</managed-bean-name>
        <managed-bean-class>p.DiscountCodeJpaController</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
automatically added in faces-config.

Spring isn't directly supported by jpa generation and you may need to add similr information manually. Looks more like feature/enhancement request to support Spring.
Comment 6 roti 2010-02-24 07:45:05 UTC
I haven't mention Spring. It is a simple jee6 web project, using just jsf.
I try to provide a sample project.
Comment 7 Sergey Petrov 2010-02-24 08:14:38 UTC
I see spring controller on http://forums.sun.com/thread.jspa?threadID=5423288
Comment 8 roti 2010-02-24 08:16:56 UTC
sorry, I just linked that forumpost because of the same errormessage
Comment 9 roti 2010-02-24 08:17:17 UTC
sorry, I just linked that forumpost because of the same errormessage, so it is not spring specific
Comment 10 Sergey Petrov 2010-02-24 08:48:41 UTC
anyway project sample is welcomed, also if you use jsf, you can verify if you have faces-config with appropriate information, it should be added to faces-config by jsf from entity wizard (if I remember right) but if you create jsf manually, you should update faces config manually too.
Comment 11 roti 2010-02-24 15:47:07 UTC
I use @Model at the managedbean, and @Inject to inject the controller.
Only have an empty beans.xml to enable injecting.
Comment 12 roti 2010-02-24 15:47:48 UTC
Created attachment 94496 [details]
sample with the sample database
Comment 13 Sergey Petrov 2010-02-25 04:21:52 UTC
Ok, tried and see the issue, but what is expected from nb? 
I don't have a solution but it looks like coding issue instead on nb issue, something is missed either unmanaged or have separate context.
I also tried to create jsf from this entity and got full list for manufacturer without any problems.
Comment 14 roti 2010-02-25 06:22:05 UTC
You are right, it is not nb's fault.
When I generate jsf page rom entity classes, it works ok.
It uses @ManagedBean and @EJB to inject.
(there are lot to learn with annotaions and cdi)
Comment 15 obrienmi 2011-03-03 18:34:54 UTC
>The workaround for DI 101 and bug# 338837 to fix the IAE issue for Criteria/Metamodel users attempting to use an EntityType that is not found is to specify the location of the classes (usually not required for Java EE environments)
This environment will exist in Java SE, Spring and certain implementations of Java EE 6 Web Profile.

Exception in thread "main" java.lang.IllegalArgumentException: The type [null] is not the expected [EntityType] for the key class [class org.eclipse.persistence.example.distributed.collatz.model.UnitOfWork].

http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_101:_20100218:_Descriptor.javaClass_is_null_on_a_container_EM_for_a_specific_case

>workaround is
<exclude-unlisted-classes>false</exclude-unlisted-classes>
or
<class>org.eclipse.persistence.example.distributed.collatz.model.UnitOfWork</class>


20110303: We have reproduced this issue in bug# 338837 in a Java SE environment. We have two workarounds and will be modifying the code to warn about the actual cause - which is a not fully specified data model in persistence.xml. 

The following specification mandated JPA 2.0 IllegalArgumentException occurs because entity classes were not processed because they are missing from the search path or not specified. 
This usually occurs in Java SE environments only. 
This is the result of an incomplete specification of the entities in persistence.xml for Java SE RESOURCE_LOCAL environments and possibly Java EE 6 Web Profile JTA environments. 
Occurs when running JPA 2.0 Criteria or Metamodel code where we require an EntityType like the following 
EntityType unitOfWork_ = getEntityManager().getMetamodel().entity(UnitOfWork.class);
        Root<UnitOfWork> root =  getEntityManager().getCriteriaBuilder().createQuery().from(UnitOfWork.class);DDLGenerationClient [Java Application]    
    org.eclipse.persistence.example.jpa.server.common.DDLGenerationClient at
localhost:4353    
        Thread [main] (Suspended)    
            MetamodelImpl.entity(Class<X>) line: 160    
            CriteriaQueryImpl<T>(AbstractQueryImpl<T>).from(Class<X>) line: 97  
            DDLGenerationClient.checkMetamodel() line: 436    
            DDLGenerationClient.doQuery() line: 460    
            DDLGenerationClient.main(String[]) line: 445    
in 
public <X> EntityType<X> entity(Class<X> clazz) {
        Object aType = this.entities.get(clazz);
        if(aType instanceof EntityType) {
            return (EntityType<X>) aType;
        } else {
-->         throw new IllegalArgumentException(ExceptionLocalization.buildMessage(We see the following exception - which is not very descriptive of the actual issue of missing metadata because of missing entity classes found during EntityManager predeploy 
[EL Finest]: 2011-03-03
12:14:36.141--ServerSession(11582167)--Thread(Thread[main,5,main])--End deploying Persistence Unit dao.create.tables.derby.client; session file:/C:/view_w36a/examples/org.eclipse.persistence.example.jpa.server.common.ddlgen/bin/_dao.create.tables.derby.client;state Deployed; factoryCount 1
Exception in thread "main" java.lang.IllegalArgumentException: 
The type [null] is not the expected [EntityType] for the key class [class org.eclipse.persistence.example.distributed.collatz.model.UnitOfWork].
    at
org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.entity(MetamodelImpl.java:160)
    at
org.eclipse.persistence.example.jpa.server.common.DDLGenerationClient.checkMetamodel(DDLGenerationClient.java:435)
    at
org.eclipse.persistence.example.jpa.server.common.DDLGenerationClient.doQuery(DDLGenerationClient.java:459)
    at
org.eclipse.persistence.example.jpa.server.common.DDLGenerationClient.main(DDLGenerationClient.java:444)
using 
<persistence-unit name="dao.create.tables.derby.client"
transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <!-- exclude-unlisted-classes>
false</exclude-unlisted-classes-->
       <!-- class>
org.eclipse.persistence.example.distributed.collatz.model.UnitOfWork</class-->
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
            <property name="eclipselink.target-database" value="Derby"/>        
            <property name="javax.persistence.jdbc.url" value="jdbc:derby://127.0.0.1:1527/dataparallel;create=true"/>
            <property name="javax.persistence.jdbc.user" value="APP"/>
            <property name="javax.persistence.jdbc.password" value="APP"/>
            <property name="eclipselink.logging.level" value="ALL"/>            
            <!-- property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

            <property name="eclipselink.ddl-generation.output-mode" value="both"/-->
        </properties>
    </persistence-unit>workaround is 
<exclude-unlisted-classes>false</exclude-unlisted-classes>
or 
<class>org.eclipse.persistence.example.distributed.collatz.model.UnitOfWork</class>
Result is OK with either fix 
unitOfWork_ EntityType: EntityTypeImpl@672904:UnitOfWork [ javaType: class org.eclipse.persistence.example.distributed.collatz.model.UnitOfWork descriptor: RelationalDescriptor(org.eclipse.persistence.example.distributed.collatz.model.UnitOfWork--> [DatabaseTable(EL_UNITOFWORK)]), mappings: 7]
Solution: 
There are two ways into this problem. 
1) We specify no entities at all and also do not specify 
<exclude-unlisted-classes>false</exclude-unlisted-classes> 
- the result of this is a completely empty metamodel 
2) We specify only part of the entities via <class> properties 
- the result of this is selective IllegalArgumentException errors thrown when we attempt to use the missing EntityType classes - likely in a Criteria API query 
We need to warn both for no (0) entities found and later that a specific type was not found - possibly as a result of a missing class reference 
1) warn the user that no entities were found during the predeploy so the deploy iterates nothing during metamodel.initialize - actually nothing is done during initialize 
MetamodelImpl@21350241 [ 0 Types: , 0 ManagedTypes: , 0 EntityTypes: , 0 MappedSuperclassTypes: , 0 EmbeddableTypes: ] 
or in 
protected void initializeCanonicalMetamodel(Metamodel model) 
DDLGenerationClient [Java Application]    
    org.eclipse.persistence.example.jpa.server.common.DDLGenerationClient at
localhost:3057    
        Thread [main] (Suspended)    
            MetamodelImpl.initialize() line: 363    
            MetamodelImpl.<init>(DatabaseSession) line: 109    
            MetamodelImpl.<init>(EntityManagerSetupImpl) line: 128    
            EntityManagerSetupImpl.getMetamodel() line: 2209    
            EntityManagerSetupImpl.deploy(ClassLoader, Map) line: 413    
            EntityManagerFactoryImpl.getServerSession() line: 185    
            EntityManagerFactoryImpl.getMetamodel() line: 531    
            DDLGenerationClient.initialize(String) line: 137    
            DDLGenerationClient.doQuery() line: 453    
            DDLGenerationClient.main(String[]) line: 444    
Descriptors map is empty - we need to warn the user about this 
this    MetamodelImpl  (id=37)    
    session    ServerSession  (id=40)    
        descriptors    HashMap<K,V>  (id=61)    
            size    0    
2) Change the IAE for a single missing entity to suggest that the entity class may not have been found or specified (it may be on purpose) 
Security note: A search for a non-existent entity class may come through a user-generated query and may be a way of testing the system for hidden classes 
- we may want to not print the name 
The error message needs to change from 
The type [null] is not the expected [EntityType] for the key class [class org.eclipse.persistence.example.jpa.server.business.Cell].
to something like 
No [EntityType] was found for the key class [class org.eclipse.persistence.example.distributed.collatz.model.UnitOfWork] in the Metamodel 
- please verify that the Entity class was referenced in persistence.xml using a specific
<class>org.eclipse.persistence.example.distributed.collatz.model.UnitOfWork</class>
property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.

Michael O'Brien
http://www.eclipselink.org
Comment 16 obrienmi 2011-03-03 18:44:33 UTC
>See EclipseLink bug
http://bugs.eclipse.org/338837