6 Replies Latest reply on Dec 26, 2012 5:33 PM by Nicholas DiPiazza

    persistence unit not found in EAR application

    Nicholas DiPiazza Novice

      I’m having a problem with JPA Persistence unit resolution in an EAR file.

       

      My ear contains one EJB and two web applications.

       

      • EAR.ear
        • lib directory
        • ejb.jar
        • WebApp1.war
        • WebApp2.war

       

      The EJB contains Entity Beans that WebApp1 and WebApp2 will both need to share.

       

      So I need to create a persistence context that each WebApp1 and WebApp2 can utilize.

       

      I found from the forums that the way to do this is to create a JAR in the ear’s lib directory that contains META-INF/persistence.xml. So here is my attempt #1:

       

      • EAR.ear
        • lib directory
          • my-persistence.jar
            • META-INF/persistence.xml
              • Creates persistence unit “myunit”
                • Set <jar-file> to ../ejb.jar
        • ejb.jar
        • WebApp1.war
          • Call EntityManagerFactory create EM with persistence unit “myunit”
        • WebApp2.war
          • Call EntityManagerFactory create EM with persistence unit “myunit”

       

      This does not work. When I call the entitymanager factory to create the entity manager, I get an error “cannot find persistence unit myunit.”

       

      So in desperation I tried approach #2:

       

      • EAR.ear
        • ejb.jar
        • WebApp1.war
          • META-INF/persistence.xml
            • Creates persistence unit “myunit1”
              • Set <jar-file> to ../../ejb.jar
          • Call EntityManagerFactory create EM with persistence unit “myunit1”
        • WebApp2.war
          • META-INF/persistence.xml
            • Creates persistence unit “myunit2”
              • Set <jar-file> to ../../ejb.jar
          • Call EntityManagerFactory create EM with persistence unit “myunit2”

       

      Same problem, still can’t find the persistence unit.

       

      I’m going to set org.jboss.as.jpa to DEBUG, maybe the debug information will help?

       

      Anything I’m doing wrong?

        • 1. Re: persistence unit not found in EAR application
          Aristides Neto Newbie

          Hi Nicholas,

           

                I´ve a setup that look like yours, what I did to make it work was creating a project jar just to keep the persistence (like you). This jars is added to the lib dir of the EAR, but the path for the jar in mine is like this:

           

          <persistence-unit name="main-pu">

                  <jta-data-source>qqo/jdbc/OracleDS</jta-data-source>

                  <jar-file>KernelCMP.jar</jar-file>

                   ....

           

           

                This KernelCMP is one of the my EJB modules. The ear structure is like this:

           

          • QQO.ear
            • lib
              • persistence.jar
                • META-INF
                  • persistence.xml
            • KernelCMP.jar
            • ...
          • 2. Re: persistence unit not found in EAR application
            Nicholas DiPiazza Novice

            Hi aristodes,

             

            Thanks for the response.

             

            Question: are you creating multiple entity managers with the same persistence unit like I am trying to do?

             

            I will try jar-file without the ../ and see if that helps.

            • 3. Re: persistence unit not found in EAR application
              Aristides Neto Newbie

              Yes, I have 5 modules using the same PU, that´s why I create a separate persistence.jar in the first place. I just simplified the structure for the post.

              • 4. Re: persistence unit not found in EAR application
                Nicholas DiPiazza Novice

                Right. Figured I would verify. Thanks.

                • 5. Re: persistence unit not found in EAR application
                  Nicholas DiPiazza Novice

                  Artistides,

                   

                  Well I found what was making the persistence.xml in the lib directory be ignored. It was the fact I was deploying through mvn jboss-as:deploy (my ear project).

                   

                  When I instead deployed by mvn clean install, then copied the EAR file created over to the deploy directory... it deployed without the "YourEntity is not mapped" error. Making me clearly see that yes, the persistence Unit was picked up.

                   

                  Now I get an NPE from hibernate!!!

                   

                  15:11:09,400 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/vosa-socialauth].[vosa-socialauth]] (http-localhost-127.0.0.1-8080-4) Servlet.service() for servlet vosa-socialauth threw exception: java.lang.NullPointerException

                      at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                      at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                      at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                      at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1207) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

                      at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:176) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

                      at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:89) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

                      at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:125) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

                      at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:120) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

                   

                  What the heck

                  • 6. Re: persistence unit not found in EAR application
                    Nicholas DiPiazza Novice

                    OK to conclude this for anyone who happens upon this post:

                     

                    My problem was not totally resolved by placing the shared persistence unit in a jar within the EAR's lib directory. This made WebApp1.war to find it, but WebApp2.war did not.

                     

                    I didn't realize that WebApp2.war is actually a spring application that uses spring beans and the spring-mvc stuff. That seemed to be the only factor that made WebApp2.war different than WebApp1.war. So Crap!

                     

                    I never fixed this, and ended up just moving to the SpringJdbc template stuff for the spring app writing and reading directly to and from the tables that were created automatically from the Hibernate entity being loaded from JPA enabled WebApp1.war. Easy enough for my situation. Glad I had that option.

                     

                    So long story short... Spring3 + entity manager that uses a shared persistence unit = no good so far. I'll try to come up with an example appliation that reproduces the problem so i can open something up on the issue tracker. That will be the end of it.

                     

                    Thanks anyone who took a look at this.