4 Replies Latest reply on Jul 21, 2008 12:54 PM by nbhatia

    Persisting POJO entities defined in a different jar

    nbhatia

      I have an existing jar file containing POJO entities (no JPA annotations, no persistence.xml). I would like to persist there entities using a new EJB jar file which contains a persistence.xml and a mapping file. Is this possible? I tried it, but I am getting a InvalidMappingException:

      Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource mappings.xml
       at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
       at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:910)
       ... 101 more
      Caused by: org.hibernate.AnnotationException: Unable to load class defined in XML: samples.estorejpa.domain.Customer
       at org.hibernate.cfg.AnnotationConfiguration.add(AnnotationConfiguration.java:592)
       at org.hibernate.cfg.AnnotationConfiguration.addInputStream(AnnotationConfiguration.java:674)
       at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
       ... 102 more
      Caused by: java.lang.ClassNotFoundException: No ClassLoaders found for: samples.estorejpa.domain.Customer
       at org.jboss.mx.loading.LoadMgr3.beginLoadTask(LoadMgr3.java:212)
       at org.jboss.mx.loading.RepositoryClassLoader.loadClassImpl(RepositoryClassLoader.java:521)
       at org.jboss.mx.loading.RepositoryClassLoader.loadClass(RepositoryClassLoader.java:415)
       at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
       at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
       at java.lang.Class.forName0(Native Method)
       at java.lang.Class.forName(Class.java:247)
       at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:112)
       at org.hibernate.reflection.java.JavaXFactory.classForName(JavaXFactory.java:153)
       at org.hibernate.cfg.AnnotationConfiguration.add(AnnotationConfiguration.java:589)
       ... 104 more
      


      Here are the relevant files in my ear. estore-common-1.0.jar contains the POJO entities. estore-server-1.0.jar is the EJB jar that contains persistence.xml, mappings.xml and and a stateless session bean called OrderServiceBean. I was hoping that the POJO entity classes would be visible when parsing persistence.xml because they are in the classpath, but that does not seem to be true.

      estore-1.0.ear
      |---estore-common-1.0.jar
      |---estore-server-1.0.jar
      | |---mappings.xml
      | |---META-INF
      | | |---ejb-jar.xml
      | | |---jboss.xml
      | | `---persistence.xml
      | `---samples
      | `---estorejpa
      | `---service
      | |---OrderService.class
      | `---OrderServiceBean.class
      `---META-INF
       application.xml
      


      Here's my persistence.xml:

      <persistence>
       <persistence-unit name="EstorePersistenceUnit">
       <jta-data-source>java:/estore</jta-data-source>
       <mapping-file>mappings.xml</mapping-file>
       <properties>
       <property name="jboss.entity.manager.factory.jndi.name" value="java:/persistence-units/EstorePersistenceUnit"/>
       </properties>
       </persistence-unit>
      </persistence>
      


      Here's my mappings.xml:

      <entity-mappings>
       <description>eStore JPA Mappings</description>
       <package>samples.estorejpa.domain</package>
       <entity class="samples.estorejpa.domain.Customer" name="Customer">
       <table name="CUSTOMER"/>
       <attributes>
       <id name="id">
       <generated-value/>
       </id>
       <basic name="version"/>
       <basic name="name"/>
       </attributes>
       </entity>
       ...
      </entity-mappings>
      


      Any help would be much appreciated.

      Thanks.
      Naresh

        • 1. Re: Persisting POJO entities defined in a different jar
          jaikiran

          Please post the contents of your application.xml.

          • 2. Re: Persisting POJO entities defined in a different jar
            nbhatia

            Here you go...

            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE application PUBLIC
             "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
             "http://java.sun.com/dtd/application_1_3.dtd">
            <application>
             <display-name>estore-app</display-name>
             <module>
             <ejb>estore-server-1.0.jar</ejb>
             </module>
            </application>
            


            • 3. Re: Persisting POJO entities defined in a different jar
              jaikiran

               

              estore-1.0.ear
              |---estore-common-1.0.jar


              Your estore-common-1.0.jar is not declared as a module in the application.xml and also its not in the default classpath of the EAR.

              If you are using JBoss-4.2.x then create a lib folder at the root of the EAR and move the estore-common-1.0.jar to that folder:
              estore-1.0.ear
               |
               |--- lib
               | |
               | |--- estore-common-1.0.jar


              Or you can add a java module to the application.xml:
              <?xml version="1.0" encoding="UTF-8"?>
              <!DOCTYPE application PUBLIC
               "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
               "http://java.sun.com/dtd/application_1_3.dtd">
              <application>
               <display-name>estore-app</display-name>
               <module>
               <java>estore-common-1.0.jar</java>
               </module>
               <module>
               <ejb>estore-server-1.0.jar</ejb>
               </module>
              </application>


              I would recommend the lib folder approach, since any jar in that folder will become available to the entire EAR.




              • 4. Re: Persisting POJO entities defined in a different jar
                nbhatia

                That worked! Thanks a ton Jaikiran.