5 Replies Latest reply on Aug 4, 2011 5:57 AM by guinotphil

    PersistenceContext injection from a different jar

    guinotphil

      Hi,

       

      I'm trying to deploy an application with many EJB/WAR modules.

       

      One of them defines the persistence entities, as well as the persistence.xml file:

       

      <?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_2_0.xsd"

                   version="2.0">

      <persistence-unit name="entityManagerFactory" transaction-type="JTA">

          <provider>org.hibernate.ejb.HibernatePersistence</provider>

          <jta-data-source>java:jboss/datasources/OracleDS</jta-data-source>

          <properties>

              <property name="jboss.as.jpa.providerModule" value="org.hibernate3"/>

      ...

      ...

      ...

          </properties>

      </persistence-unit>

       

      Then, in other modules, I've got to refer the entityManagerFactory. But,it gives me the following error:

       

      Can't find a deployment unit named entityManagerFactory at subdeployment "EJB_Module.jar" of deployment "EAR_Project.ear"

       

      I've tryied to replace

      @PersistenceContext(unitName="entityManagerFactory")

      by

      @PersistenceContext(unitName="EJB_DBModule.jar#entityManagerFactory")

      or

      @PersistenceContext(unitName="../EJB_DBModule.jar#entityManagerFactory")

       

      But, I still get the same error.

       

       

       

      I've tried the workaround mentionned here : http://community.jboss.org/thread/2111 and created in each module a persistence.xml file which refers the entities jar file :

       

      <?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_2_0.xsd"

                   version="2.0">

         <persistence-unit name="entityManagerFactory">

           <jar-file>EJB_DBModule.jar</jar-file>

         </persistence-unit>

      </persistence>

       

      But, it actually doesn't seem to load configuration from the jar file and so doesn't connect me to the database:

       

      org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available

       

       

      How can I share my entity manager between all the modules ? Should I duplicate all the configuration from persistence.xml in every module ?

       

      Thank you very much


        • 1. Re: PersistenceContext injection from a different jar
          jaikiran

          Moved to AS7 user forum

          • 2. Re: PersistenceContext injection from a different jar
            prasad.deshpande

            Does this module (I presume a jar file) contains only entities/helper classes & persistence.jar? If so, try moving it to ear/lib folder, persistence unit will be available to all your ejb/war modules.

            • 3. Re: PersistenceContext injection from a different jar
              smarlow

              Also enable TRACE logging for org.jboss.as.jpa (in as/standalone/configuration/standalone.xml) to enable logging of the PU search. 

              • 4. Re: PersistenceContext injection from a different jar
                fabricio.lemos

                I have the same problem, the only difference is that I deploy a WAR instead of an EAR.

                 

                How are you deploying your app? I figured out that the error only occur if I deploy through JBoss Tools 3.3M2 (with Eclipse Indigo). If I deploy through Jboss AS admin page it does work. I still don't know what's wrong with Jboss Tools.

                • 5. Re: PersistenceContext injection from a different jar
                  guinotphil

                  Hi !

                   

                  I've moved my persistence module as a java module deployed in lib.

                   

                  The persistence.xml is still in this module's META-INF.

                   

                  When I start the server with a LOG logging for org.jboss.as.jpa, I see that it maps the entities :

                   

                  11:17:59,265 INFO  [org.hibernate.cfg.AnnotationBinder] (MSC service thread 1-1) Binding entity from annotated class: test.mymodel.Member

                  11:18:02,306 DEBUG [org.hibernate.cfg.Configuration] (MSC service thread 1-1) resolving reference to class: test.mymodel.Member

                  11:18:03,207 DEBUG [org.hibernate.ejb.event.CallbackResolver] (MSC service thread 1-1) Adding prePersist as PrePersist callback for entity test.mymodel.Member.

                  11:18:04,729 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] (MSC service thread 1-1) Static SQL for entity: test.mymodel.Member

                  11:18:17,146 DEBUG [org.hibernate.loader.entity.EntityLoader] (MSC service thread 1-1) Static select for entity test.mymodel.Member [NONE]: ....

                  11:18:17,148 DEBUG [org.hibernate.loader.entity.EntityLoader] (MSC service thread 1-1) Static select for entity test.mymodel.Member [UPGRADE]: ...

                  ...

                   

                  But when deploying the war module, I've got the following exception:

                   

                   

                   

                  11:18:32,724 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/context]] (MSC service thread 1-2) Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.jboss.seam.servlet.SeamListener: org.jboss.seam.InstantiationException: Could not instantiate Seam component: entityManagerFactory

                      at org.jboss.seam.Component.newInstance(Component.java:2144) [jboss-seam.jar:]

                      at org.jboss.seam.contexts.Contexts.startup(Contexts.java:304) [jboss-seam.jar:]

                      at org.jboss.seam.contexts.Contexts.startup(Contexts.java:278) [jboss-seam.jar:]

                      at org.jboss.seam.contexts.ServletLifecycle.endInitialization(ServletLifecycle.java:116) [jboss-seam.jar:]

                      at org.jboss.seam.init.Initialization.init(Initialization.java:740) [jboss-seam.jar:]

                      at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:36) [jboss-seam.jar:]

                      at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3368) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1-SNAPSHOT]

                      at org.apache.catalina.core.StandardContext.start(StandardContext.java:3821) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1-SNAPSHOT]

                      at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:70) [jboss-as-web-7.1.0.Alpha1-SNAPSHOT.jar:7.1.0.Alpha1-SNAPSHOT]

                      at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1765)

                      at org.jboss.msc.service.ServiceControllerImpl$ClearTCCLTask.run(ServiceControllerImpl.java:2291)

                      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_22]

                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_22]

                      at java.lang.Thread.run(Thread.java:662) [:1.6.0_22]

                  Caused by: javax.persistence.PersistenceException: [PersistenceUnit: entityManagerFactory] Unable to configure EntityManagerFactory

                      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:378) [hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]

                      at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56) [hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]

                      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) [hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final]

                      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) [hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final]

                      at org.jboss.seam.persistence.EntityManagerFactory.createEntityManagerFactory(EntityManagerFactory.java:81) [jboss-seam.jar:]

                      at org.jboss.seam.persistence.EntityManagerFactory.startup(EntityManagerFactory.java:50) [jboss-seam.jar:]

                      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_22]

                      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_22]

                      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_22]

                      at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_22]

                      at org.jboss.seam.util.Reflections.invoke(Reflections.java:22) [jboss-seam.jar:]

                      at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144) [jboss-seam.jar:]

                      at org.jboss.seam.Component.callComponentMethod(Component.java:2257) [jboss-seam.jar:]

                      at org.jboss.seam.Component.callCreateMethod(Component.java:2172) [jboss-seam.jar:]

                      at org.jboss.seam.Component.newInstance(Component.java:2132) [jboss-seam.jar:]

                      ... 13 more

                  Caused by: org.hibernate.MappingException: Cannot cache an unknown entity: test.mymodel.Member

                      at org.hibernate.cfg.Configuration.applyCacheConcurrencyStrategy(Configuration.java:2796) [hibernate-core-3.6.6.Final.jar:3.6.6.Final]

                      at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1405) [hibernate-core-3.6.6.Final.jar:3.6.6.Final]

                      at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375) [hibernate-core-3.6.6.Final.jar:3.6.6.Final]

                      at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1519) [hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]

                      at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193) [hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]

                      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1100) [hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]

                      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:282) [hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]

                      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:366) [hibernate-entitymanager-3.6.6.Final.jar:3.6.6.Final]

                      ... 27 more

                   

                  Seems like it cannot find the entities...

                   

                   

                  @Fabricio: have you noticed any difference in your deployments when you deploy with JBossTools or the admin page ?