5 Replies Latest reply on Oct 4, 2010 6:46 AM by Rob Shepherd

    Unknown entity

    Christoph Schmidl Newbie
      hey there


      I`m using seam 2 with hibernate, ejb3, richfaces, ibm websphere and Rational Software Architect as my IDE (based on eclipse) and I tried to test my persistence entity packages, which are all located in an extra project.
      I've created a new project with testng, which just should contain test-classes, who are referencing the entity classes in the persistence project, initiate them and run the tests... but it seems, it's not that easy. I added the imports in the test-classes to the entities in the persistence-project and all seems normal, no errors. I wrote a test, which just initiates the persistence entity, persist it to the oracle db, reads them out trough the entitiy-manager by using "find()" and remove the entity again from the db.
      And here comes the error: at the point, where the entitiy-manager tries to persist the entity, I get the following error:



      java.lang.IllegalArgumentException: Unknown entity: de.thehive.entity.staticData.Client
              at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:216)
              at de.thehive.entity.staticData.test.ClientTest.testClientPersistence(ClientTest.java:38)
      ... Removed 22 stack frames



      It seems like the application finds the class, otherwise the import wouldn't have worked, but when it tries to persist, it doesn't have the mapping, doesn't have the class in the runtime classpath or whatever. Maybe it's because there is a persistence.xml in the entity-package and an extra persistence.xml in the testng-project?

      here's the persistence.xml from the testng-proect. I also tried to publish the class by adding the "<class>"-tag to the xml-file, but it cannot resolve the name? weird..






      <?xml version="1.0" encoding="UTF-8"?>
      <persistence 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_1_0.xsd"
              version="1.0">
              <persistence-unit name="xxx"
                      transaction-type="RESOURCE_LOCAL">
                      <provider>org.hibernate.ejb.HibernatePersistence</provider>
                      <!-- <class>de.thehive.entity.staticData.Client</class> -->
                      <properties>
                              <property name="hibernate.show_sql" value="false" />
                              <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
                              <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
                              <property name="hibernate.transaction.manager" value="org.hibernate.transaction.JDBCTransactionFactory" />
                              <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
                              <property name="hibernate.archive.autodetection" value="class, hbm" /> 
                              <!--
                                      Insert your url like jdbc:oracle:thin:@localhost:port:sid
                                      Insert the database username
                                      Insert the database password
                              -->
                              <property name="hibernate.connection.url" value="jdbc:oracle:thin:@xxx.de:1521:xx" />
                              <property name="hibernate.connection.username" value="xxx" />
                              <property name="hibernate.connection.password" value="xxx" />
                      </properties>
              </persistence-unit>
      </persistence>





      and the persistence.xml from the entity-project:



      <?xml version="1.0" encoding="UTF-8"?>
      <persistence 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_1_0.xsd"
              version="1.0">
              <persistence-unit name="xxx"
                      transaction-type="JTA">
                      <provider>org.hibernate.ejb.HibernatePersistence</provider>
                      <jta-data-source>jdbc/xxx</jta-data-source>
                      <mapping-file>org/jbpm/hibernate.wire.hbm.xml</mapping-file>
                      <mapping-file>org/jbpm/hibernate.definition.hbm.xml</mapping-file>
                      <mapping-file>org/jbpm/hibernate.queries.hbm.xml</mapping-file>
                      <mapping-file>org/jbpm/hibernate.execution.hbm.xml</mapping-file>
                      <mapping-file>org/jbpm/hibernate.type.hbm.xml</mapping-file>
                      <mapping-file>org/jbpm/hibernate.job.hbm.xml</mapping-file>
                      <mapping-file>de/thehive/approval/task/approval_process.hbm.xml</mapping-file>
                      <properties>
                              <!-- property name="hibernate.hbm2ddl.auto" value="create-drop"/-->
                              <property name="hibernate.show_sql" value="false" />
                              <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
                              <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
                              <!-- <property name="hibernate.default_schema" value="xxx" /> -->
                              <property name="hibernate.transaction.manager_lookup_class"
                                      value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
                      </properties>

              </persistence-unit>
      </persistence>



      any suggestions? ;)
        • 1. Re: Unknown entity
          Christoph Schmidl Newbie

          i changed some project-dependencies and added the required class by using the class-tag in the persistence.xml. it works that way, my tests run without any errors, but isn't there a more sophisticated way available for adding entities? I don't want to add hundreds of classes to the xml-file just because I want to reach them through my tests :(

          • 3. Re: Unknown entity
            Rob Shepherd Newbie

            did you look at the @Entity annotation for your entities?


            http://docs.jboss.org/hibernate/stable/annotations/reference/...


            Best regards


            Rob

            • 4. Re: Unknown entity
              Christoph Schmidl Newbie
              the entities in itself are all fine. when i create a test-class inside the entity-project, they all work and the tests pass. but when i create test-classes in another project and try to reference the entities and start the tests, I get the "unknown entity"-exception, when the entity-manager wants to perform the persist-method. by now the only solution is to add the entities manually by using the <class>-tag, but that's just a horrible solution, if I want to test hundreds of entities.
              • 5. Re: Unknown entity
                Rob Shepherd Newbie

                What is providing the EntityManager in your TestNG project?   Are you using a J2EE app server, the SEAM2 bootstrapped embedded JBoss environment, a SEAM War project or some handrolled solution?


                When the application starts, with hibernate debug-logging enabled, you will see output from all the Entites it has processed, and thus are Hibernate/JPA entities ongoing in the app; rather than just classes.


                This phase is obviousley not happening in your test environment.  Either SEAM, Hibernate or J2EE/JPA is not starting (properly) or the relavant thing in this list is not finding your annotated entities in your entity library when it starts.
                It will need to be available to the correct classloader.


                Where this actually is depends on the answers to the first query above.


                Hope this helps a bit.


                Rob