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 180810 - generated AbstractFacade.count() does not work
Summary: generated AbstractFacade.count() does not work
Status: RESOLVED FIXED
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 6.x
Hardware: All All
: P3 normal (vote)
Assignee: Andrey Yamkovoy
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-02-16 18:07 UTC by David Konecny
Modified: 2011-03-03 20:28 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
exception (5.48 KB, text/plain)
2010-02-16 18:08 UTC, David Konecny
Details
TestCase_10810 (34.38 KB, application/octet-stream)
2010-02-17 02:27 UTC, Milan Kuchtiak
Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Konecny 2010-02-16 18:07:37 UTC
Andrey, I assume that you updated AbstractFacade to use Criteria API instead of SQL string which is great and thanks for that but it does not work - generate JPA and EJB Facade and call count() on the facade EJB and you will get attached exception.
Comment 1 David Konecny 2010-02-16 18:08:59 UTC
Created attachment 94215 [details]
exception
Comment 2 Sergey Petrov 2010-02-16 23:32:36 UTC
I see Caused by: java.lang.IllegalArgumentException: The type [null] is not the expected [EntityType] for the key class [class cust.DiscountCode].

Do you have DiscountCode generated properly? Do you have exclude unlisted classes in persistence.xml?
Comment 3 David Konecny 2010-02-17 01:29:24 UTC
DC class is generated and persistence.xml contains

  <persistence-unit name="WebApp1PU" transaction-type="JTA">
    <jta-data-source>jdbc/sample</jta-data-source>
    <properties/>
  </persistence-unit>

Test it yourself and let me know if it works for you.
Comment 4 Milan Kuchtiak 2010-02-17 02:27:13 UTC
Created attachment 94220 [details]
TestCase_10810
Comment 5 Milan Kuchtiak 2010-02-17 02:28:37 UTC
I have the same exception when testing the scenario described in 180764:

Scenario:

- created JPA classes in java application (JavaApp1):
- created new web application(WebApp1) and added JavaApp1 dependency
- created session bean from entity class in WebApp1

See the attached projects above.
Comment 6 Milan Kuchtiak 2010-02-17 02:30:37 UTC
Note: this happens not only when calling count() method, but any business method from AbstractFacade class.
Comment 7 Andrey Yamkovoy 2010-02-26 08:29:35 UTC
David I can't reproduce the problem you mentioned using the following steps:
- Create WebApp
- Generate Entity
- Generate Session facade.
- Call facade's count() from the servlet.

But I can reproduce it using the project which Milan attached.
So as far as I see the problem is the entities are not in the persistence context. If include the entity classes into the WebApp's persistence.xml everything works fine. 
In other words you just need to changed the persistence.xml with the following:

  <persistence-unit name="WebApp1PU" transaction-type="JTA">
    <jta-data-source>jdbc/sample</jta-data-source>
    <class>cust.Customer</class>
    <class>cust.DiscountCode</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>

Or as alternative you can change the generated facades to use persistence unit "JavaLib1PU" and change it with the following:

  <persistence-unit name="JavaLib1PU" transaction-type="JTA">
    <jta-data-source>jdbc/sample</jta-data-source>
    <class>cust.Customer</class>
    <class>cust.DiscountCode</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
Comment 8 Sergey Petrov 2010-02-26 09:01:39 UTC
It's the same as issue 181068, in case of issue #181068 I was sure something is wrong with user created code, but may be I miss something.

In this issue I see no issue but some dependencies on #101556 
and may be some issues on gf/eclipselink side with problems to create proper persistence context for second persistence.xml as it looks strange for me to have fix with explicit declaration for classes when it should work for containers because no <eclude-unlisted-classes/> is specified in my opinion, but jpa  spec may need to be double checked.

David, have you tried to update code to not use abstract facade? As this issues seems to be unrelated to abstract facade usage. Also was it initially filed for case attached later?
Comment 9 Sergey Petrov 2010-02-26 09:06:06 UTC
In general this exception "The type [null] is not the expected [EntityType]" is valid exception for number of cases, haven't tried yet but in my opinion it should be thrown in case if j2se have pu with   <exclude-unlisted-classes>true</exclude-unlisted-classes> but entity isn't listed in this pu.
Comment 10 David Konecny 2010-02-28 16:11:28 UTC
You are right guys. I filed this while working on different problem and it was based on Milan's usecase where entities are in different jar - I cannot reproduce it if entities are defined directly within the WAR. Sorry for false alarm. If persistence.xml is defined in WAR's WEB-INF/classes then JPA scans only WEB-INF/classes for available entities. If entities are placed in a JAR file which is packaged in WAR's WEB-INF/lib folder then:
* as was mentioned, entities has to be enumerated in persistence.xml; or
* persistence.xml can be instructed to scan additional jars for entities via <jar-file> tag, eg. <jar-file>lib/JavaLib1.jar</jar-file> in Milan's case.
(JPA spec chapters 8.2, 8.2.1.6.3, 8.2.2)
Comment 11 Petr Jiricka 2010-04-06 15:23:15 UTC
I am now also getting this error in Java EE 5 project with Tomcat as the target server. My PU looks like this:

<?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="WebApplication28PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample"/>
      <property name="javax.persistence.jdbc.password" value="app"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.user" value="app"/>
    </properties>
  </persistence-unit>
</persistence>

However, when I explicitly add:

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

then things start to work. I found the following: http://www.oracle.com/technology/products/ias/toplink/jpa/howto/java-se-usage.html

Which says: "The default value for the exclude-unlisted-classes element is true in Java SE, and false in Java EE."

What does it mean for Tomcat? Should we explicitly add the false value when running against Tomcat?
Comment 12 David Konecny 2010-04-06 22:50:41 UTC
(In reply to comment #11)
> What does it mean for Tomcat? Should we explicitly add the false value when
> running against Tomcat?

I would think that it is JPA provider which sets default value so EclipseLink JPA2 provider should behave the same regardless of a server, no?
Comment 13 vinh_truong 2010-10-05 02:19:11 UTC
I got same problem in NetBeans 6.9.1 and 6.10M1.  For learning purpose, I followed JSF CRUD app in NB example.
1/ Created a web app + Tomcat6 + JSF2.
2/ Lets NB auto generated entities from database, project -> New -> Entity Classes from database
3/ NB auto generated jsf pages, New -> JSF pages from Entity classes
4/ Run --> clicked on a Address link for first time, and got this error:

Caused by: java.lang.IllegalArgumentException: The type [null] is not the expected [EntityType] for the key class [class jpa.entities.Address].
	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 jpa.controllers.AddressJpaController.getAddressCount(AddressJpaController.java:143)



public int getAddressCount() {
   EntityManager em = getEntityManager();
   try {
     CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
     Root<Address> rt = cq.from(Address.class);     <<--- this line caused error
     cq.select(em.getCriteriaBuilder().count(rt));
     Query q = em.createQuery(cq);
     return ((Long) q.getSingleResult()).intValue();
   } finally {
      em.close();
   }
}


<?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="JSFExamplePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/exampledb"/>
      <property name="javax.persistence.jdbc.password" value="xyz"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
    </properties>
  </persistence-unit>
</persistence>

-----------------------------------------------

* Work around:
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
Comment 14 Sergey Petrov 2010-10-05 03:40:11 UTC
It's not really initial issue, the same "missed entity" can be got a lot of ways.
The only question here why generated entities wasn't included in persistence.xml with resource local transaction as for j2se projects it should be included and tomcat woks similar.
Comment 15 Sergey Petrov 2010-10-11 14:03:08 UTC
As I see the workaround is implemented in case of New PU wizard but if pu is created from entities wizard "<exclude..." isn't added, it need to  be corrected.
Comment 16 Sergey Petrov 2010-10-11 14:30:49 UTC
old issue http://netbeans.org/bugzilla/show_bug.cgi?id=142575
Comment 17 Sergey Petrov 2010-10-11 15:23:37 UTC
http://hg.netbeans.org/web-main/rev/1371f2a0cfda sync most cases of pu creation (add "exclude") but all should work before fix(in 6.10) also as classes was added to persistence.xml when I tried entities from pu generation on tomcat.
Comment 18 Quality Engineering 2010-10-12 02:32:01 UTC
Integrated into 'main-golden', will be available in build *201010120000* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/1371f2a0cfda
User: Sergey B. Petrov <sj-nb@netbeans.org>
Log: fix #180810 - synch addition of "exclude" in all places, but may need to be reviewed as now classes are added also for tomcat
Comment 19 obrienmi 2011-03-03 19:22:00 UTC
Serjey,
   We arrived at the same conclusion - we just had a very large issue around this IAE testing the upcoming PWP.  After I figured out what the issue was - I did a quick google on the error in order to put the fix everywhere and came across this bug - I should have used google first.

>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
Global to all classes
<exclude-unlisted-classes>false</exclude-unlisted-classes>
or specific to a single class
<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.

Just a note that the fix for 338837 will be to help the user figure out why they are missing entity classes during deployment (they were not found) - by adding more warnings only.  The search path for entities will still be user or EJB-container (in the case of Java EE 5) controlled.
The user may wish to split entities among persistence units - this is why control is at the user level and where we do not want the API to pick up everything (unless specified) - however it is the cause of this unfortunate situation where you attempt to use something that is not known.
In the words of a certain public figure "it is unfortunate that we don't know what we don't know"


see related
http://www.netbeans.com/projects/javaee/lists/issues/archive/2010-02/message/627
http://netbeans.org/bugzilla/show_bug.cgi?id=181068
http://stackoverflow.com/questions/3559691/jsf-crud-application-and-entities/5185057#5185057
http://netbeans.org/bugzilla/show_bug.cgi?id=180810
http://old.nabble.com/Re%3A-problem-with-criteria-The-type--null--is-not-the-expected--EntityType--for-the-key-class--class-br.com.thinkit.db.Ativos----try-upgrade-past-8141.-p30219742.html
http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg05508.html
http://forums.devshed.com/java-help-9/jpa-controller-not-work-in-ejb-706551.html
not http://forum.springsource.org/archive/index.php/t-32323.html
http://comments.gmane.org/gmane.comp.java.tynamo.user/173
http://forums.netbeans.org/post-90076.html

Michael O'Brien
http://www.eclipselink.org
Comment 20 Sergey Petrov 2011-03-03 20:28:32 UTC
Hi Michael, thanks for update on these issues.