9 Replies Latest reply on Nov 6, 2010 9:12 AM by jaikiran

    Persistence unit issues when migrating to JBOSS 5.1.0

      Hello everyone,

       

      I have a seam application that used to run fine in JBOSS 4.2.3 and the ear had the following structure:

       

      myapp.ear
      +- lib/
      | +- extejbmodule.jar
      | +- ...
      +- mywebmodule.war
      +- myseammodule.jar
      | +-META-INF/
      | | +- persistence.xml
      | +- ... hibernate entities ...
      | +- ... ejbs ... | +- ...
      +- ...

       

      extejbmodule.jar has several classes that are injected (using @PersistenceContext) with the default persistence unit defined in the myseammodule.jar. When the ear is deployed to 5.0, due to the new persistence unit scope approach, the persistence unit is not visible to the extejbmodule.jar and I get :

      Can't find a persistence unit named 'null' in AbstractVFSDeploymentContext@2127445{....

      I created a new jar that only contains the persistence.xml file in its META-INF folder and I placed it in the root of the ear file. The ear structure looks like this now:

       

      myapp.ear
      +- lib/
      | +- extejbmodule.jar
      | +- ...
      +- myseammodule.jar
      | +- ... hibernate entities ...
      | +- ... ejbs ...
      +- persistence.jar
      | +-META-INF/
      | | +- persistence.xml
      | | +- ...
      +- mywebmodule.war
      +- ...
      

       

      And I defined the new module in META-INF/application.xml:

       

         <module>
            <java>persistence.jar</java>
         </module>
      

       

      With this approach both the extejbmodule.jar and the myseammodule.jar can see the persistence unit but the hibernate classes are not scanned for annotations and no entities are mapped. The log entry looks like this:

       

      14:59:51,868 DEBUG [AbstractJarVisitor] Searching mapped entities in jar/par: vfszip:/C:/work/tools/jboss-5.1.0.GA/server/default/deploy/myapp.ear/persistence.jar/
      
      

       

      It looks like the ejb container only scans the jar that contains the persistence unit definition.How can I instruct the container to scan the correct jars? Or, if my approach is not correct, what is the best way to have the persistence unit be available in the entire ear scope (available to all modules)?

       

      Thanks,

      Dragos

        • 1. Re: Persistence unit issues when migrating to JBOSS 5.1.0

          I found a way to specify which jars should be scanned for hibernate annotations. In persistence.xml:

           

             <persistence-unit name="myapp">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <jta-data-source>java:/jdbc/myapp</jta-data-source>
                <jar-file>myseammodule.jar</jar-file>
                <properties>
                    ...
             </persistence-unit>
          

           

          Now the container scans the myseammodule.jar for hibernate classes and everything works fine.

           

          I still wonder if this structure is the best way to have the persistence unit scope set to ear level. I find it strange that I have to create an empty jar just to place persistence.xml at the root level of the ear.

          Is there a better way to accomplish this?

           

          Thanks,

          Dragos

          • 2. Re: Persistence unit issues when migrating to JBOSS 5.1.0
            jaikiran

            dbobes wrote:

             

            I find it strange that I have to create an empty jar just to place persistence.xml at the root level of the ear.

            Is there a better way to accomplish this?

             

            Thanks,

            Dragos

            Place the persistence.xml in the META-INF of the .ear.

             

            So:

            myapp.ear
            |
            |--- META-INF
            |      |
            |      |--- persistence.xml
            

             

             

            And let the persistence.xml use the "jar-file" element to point to the jars containing the entities. This is how the EJB3 persistence spec lets EAR level persistence.xml to be deployed. For more details, see section 6.2 of EJB3 Persistence spec.

            • 3. Re: Persistence unit issues when migrating to JBOSS 5.1.0

              Thanks Jaikiran, it makes a lot more sense now. I tried this earlier but it failed because I wasn't using the <jar-file> tag and I ended up thinking this is not the way to do it.

              • 4. Re: Persistence unit issues when migrating to JBOSS 5.1.0
                jaikiran

                jaikiran wrote:


                Place the persistence.xml in the META-INF of the .ear.

                 

                So:

                
                myapp.ear
                |
                |--- META-INF
                |      |
                |      |--- persistence.xml
                

                 

                 

                And let the persistence.xml use the "jar-file" element to point to the jars containing the entities. This is how the EJB3 persistence spec lets EAR level persistence.xml to be deployed. For more details, see section 6.2 of EJB3 Persistence spec.

                For the record - my statement is incorrect. The EJB3 spec doesn't _not_ specify that the persistence.xml can be placed in the META-INF of the .ear. According to the spec, for scoping a persistence unit at ear level, the persistence.xml should be packaged in:

                 

                • META-INF of a jar file in the root of the EAR
                or
                • META-INF of a jar file in the EAR library directory

                 

                Note that although JBoss scans the .ear/META-INF/persistence.xml file, it's _not_ recommend that applications package their persistence.xml in this manner, since it can lead to issues like the one being discussed here http://community.jboss.org/message/520279

                • 5. Re: Persistence unit issues when migrating to JBOSS 5.1.0
                  Hi Jaikiran, I appreciate the time you spent on this issues.
                  • META-INF of a jar file in the root of the EAR

                   

                  This is how I had the ear in the first place and it didn't work.

                  If you look at my first post on this discussion the persistence.xml file was located in the META-INF folder of the myseammodule.jar which was located in the root of the ear (also this jar was specified as an ejb module in application.xml). In this case extejbmodule.jar didn't have access to the persistence unit.

                   


                  • META-INF of a jar file in the EAR library directory

                   


                  I still have to try this option but right now I encountered another scoping problem. The extejbmodule.jar (from my first post) has a stateless bean defined like this:

                   

                  @Stateless(name="Server", mappedName="myapp/Server/local")

                   

                  In the seam's components.xml file I have defined the server component like this:

                   

                      <component name="server" auto-create="true"
                                scope="event" jndi-name="myapp/Server/local"
                                class="com.company.ServerImpl">
                  
                  

                  But when I try to inject the component into a seam bean:

                   

                  @In Server server;
                  

                   

                  it throws:

                   

                  javax.naming.NameNotFoundException: Server not bound

                   

                  In debug, it fails when it tries to lookup the "myapp/Server/local" jndi name. This was not a problem in JBOSS 4.2.3 so it must be also related to the new scoping where entities from one module are not visible to the others.

                   

                  Any ideas?

                  • 6. Re: Persistence unit issues when migrating to JBOSS 5.1.0
                    jaikiran

                    dbobes wrote:

                     

                    has a stateless bean defined like this:

                     

                    @Stateless(name="Server", mappedName="myapp/Server/local")
                    

                     

                    In the seam's components.xml file I have defined the server component like this:

                     

                        <component name="server" auto-create="true"
                                  scope="event" jndi-name="myapp/Server/local"
                                  class="com.company.ServerImpl">
                    
                    

                    But when I try to inject the component into a seam bean:

                     

                    @In Server server;
                    

                     

                    it throws:

                     

                    javax.naming.NameNotFoundException: Server not bound

                     


                     

                    The mappedName attribute of @Stateless is _not_ used as the jndi name for binding into JNDI. You will have to use @RemoteBinding (or @LocalBinding) as explained here

                    • 7. Re: Persistence unit issues when migrating to JBOSS 5.1.0

                      @LocalBinding solved the lookup problem. Right now the lookup works in both JBOSS 5 and JBOSS 4.

                       

                      I am still placing the persistence.xml in the ear's META-INF as this is the only way to have the persistence unit shared between ear's modules. I just have to be careful to change the <jar-file> tag to 'myapp.ear/myseammodule.jar' when I deploy it on windows and to 'myseammodule.jar' when I deploy it in unix.

                      • 8. Re: Persistence unit issues when migrating to JBOSS 5.1.0
                        bohl

                        Sorry for waking up this old thread. I have a question about this:

                        For the record - my statement is incorrect. The EJB3 spec doesn't _not_ specify that the persistence.xml can be placed in the META-INF of the .ear. According to the spec, for scoping a persistence unit at ear level, the persistence.xml should be packaged in:

                         

                        • META-INF of a jar file in the root of the EAR
                        or
                        • META-INF of a jar file in the EAR library directory

                         

                         

                        I'm not sure which document is meant with "EJB3 spec", could you please post a link? If you meant this document http://download.oracle.com/javaee/6/api/ .., I can't find any deployment rules for .ear files in there.

                        • 9. Re: Persistence unit issues when migrating to JBOSS 5.1.0
                          jaikiran

                          Lars Bohl wrote:

                           

                          Sorry for waking up this old thread. I have a question about this:

                          For the record - my statement is incorrect. The EJB3 spec doesn't _not_ specify that the persistence.xml can be placed in the META-INF of the .ear. According to the spec, for scoping a persistence unit at ear level, the persistence.xml should be packaged in:

                           

                          • META-INF of a jar file in the root of the EAR
                          or
                          • META-INF of a jar file in the EAR library directory

                           

                           

                          I'm not sure which document is meant with "EJB3 spec", could you please post a link?

                          The Final Release document here http://jcp.org/en/jsr/detail?id=220