8 Replies Latest reply on Sep 13, 2006 4:21 AM by Nicolai Ødum

    Persistence unit scoping across multiple jars in a single ea

    celer celer Newbie

      Hi,

      I am trying to build an ear file with multiple jar files in the ear. I want to share one persistence unit across the jar files.

      I have tried putting a persistence.xml in each jar and that fails, I get an exception,

      javax.management.InstanceAlreadyExistsException: persistence.units:ear=ear.ear,unitName=foo already registered.

      After the first jar which defines the persistence unit is deployed.

      I have tried moving the persistence.xml up into the ear file, and it is not loaded at all.

      How is this typically done?

        • 1. Re: Persistence unit scoping across multiple jars in a singl
          Raja Master

          From the EJB Spec,


          A persistence unit must have a name. Only one persistence unit of any given name may be defined within a single EJB-JAR file, within a single WAR file, within a single application client jar, or within an EAR (in the EAR root or lib directory).

          And as for scope of the persistence.xml (between EJB, WEB, or EAR)

          A persistence unit that is defined at the level of the EAR is generally visible to all components in the application.


          So if you put your generic persistence.xml in the root folder(where your EAR is defined or in a lib subfolder), all your sub deployments should be able to use that.


          • 2. Re: Persistence unit scoping across multiple jars in a singl
            Raja Master

            That said, the above dint work for a trivial example for me. Can you verify this as well? (Or am i reading the spec wrong ?)

            • 3. Re: Persistence unit scoping across multiple jars in a singl
              Raja Master

              Sorry scratch my previous comment, it does work well if you put the persistence.xml in a jar file and make it a part of your archive. Note that you would have to enter the jar file containing the managed classes in the persistence.xml file.

              • 4. Re: Persistence unit scoping across multiple jars in a singl
                paata lominadze Master

                hi all, i had already problem like this and my mistake was that i hadn't unitname in the persistance.xml file
                i think if u must add this in u your persistance.xml

                 <persistence-unit name="Blah1" transaction-type="JTA">
                 <jta-data-source>java:/XAOracleDS</jta-data-source>
                 <class>com.magti.businesslayer.ejb3entity.oracle.Test</class>
                 <properties>
                 [props]
                 </properties>
                 </persistence-unit>
                
                

                in second persistanc.xml
                 <persistence-unit name="Blah2" transaction-type="JTA">
                 <jta-data-source>java:/XAOracleDS</jta-data-source>
                 <class>com.magti.businesslayer.ejb3entity.oracle.Test</class>
                 <properties>
                 [props]
                 </properties>
                 </persistence-unit>
                
                


                u also could put more then one persistance unit in one persistance.xml file
                 <persistence-unit name="Blah1" transaction-type="JTA">
                 <jta-data-source>java:/XAOracleDS</jta-data-source>
                 <class>com.magti.businesslayer.ejb3entity.oracle.Test</class>
                 <properties>
                 [props]
                 </properties>
                 </persistence-unit>
                 <persistence-unit name="Blah2" transaction-type="JTA">
                 <jta-data-source>java:/XAOracleDS</jta-data-source>
                 <class>com.magti.businesslayer.ejb3entity.oracle.Test</class>
                 <properties>
                 [props]
                 </properties>
                 </persistence-unit>
                
                


                • 5. Re: Persistence unit scoping across multiple jars in a singl
                  celer celer Newbie

                  Right, but in my tests, if you had different unit names then the entity beans couldn't have any relationships between them.

                  The only solution I have found is like raja05 said, is to put the persistence.xml in a jar file in the ear file. And then reference from that persistence.xml file all the other jars.


                  "grdzeli_kaci" wrote:
                  hi all, i had already problem like this and my mistake was that i hadn't unitname in the persistance.xml file
                  i think if u must add this in u your persistance.xml

                   <persistence-unit name="Blah1" transaction-type="JTA">
                   <jta-data-source>java:/XAOracleDS</jta-data-source>
                   <class>com.magti.businesslayer.ejb3entity.oracle.Test</class>
                   <properties>
                   [props]
                   </properties>
                   </persistence-unit>
                  
                  

                  in second persistanc.xml
                   <persistence-unit name="Blah2" transaction-type="JTA">
                   <jta-data-source>java:/XAOracleDS</jta-data-source>
                   <class>com.magti.businesslayer.ejb3entity.oracle.Test</class>
                   <properties>
                   [props]
                   </properties>
                   </persistence-unit>
                  
                  


                  u also could put more then one persistance unit in one persistance.xml file
                   <persistence-unit name="Blah1" transaction-type="JTA">
                   <jta-data-source>java:/XAOracleDS</jta-data-source>
                   <class>com.magti.businesslayer.ejb3entity.oracle.Test</class>
                   <properties>
                   [props]
                   </properties>
                   </persistence-unit>
                   <persistence-unit name="Blah2" transaction-type="JTA">
                   <jta-data-source>java:/XAOracleDS</jta-data-source>
                   <class>com.magti.businesslayer.ejb3entity.oracle.Test</class>
                   <properties>
                   [props]
                   </properties>
                   </persistence-unit>
                  
                  


                  • 6. Re: Persistence unit scoping across multiple jars in a singl
                    Jeff Brooks Newbie

                     

                    The only solution I have found is like raja05 said, is to put the persistence.xml in a jar file in the ear file. And then reference from that persistence.xml file all the other jars.


                    How exactly did you do that?

                    Can you list what settings you put into what files and show how the .ear is structured?

                    Jeff Brooks


                    • 7. Re: Persistence unit scoping across multiple jars in a singl
                      Primitivo Dimahugot Newbie

                       

                      "JeffBrooks" wrote:
                      The only solution I have found is like raja05 said, is to put the persistence.xml in a jar file in the ear file. And then reference from that persistence.xml file all the other jars.


                      How exactly did you do that?

                      Can you list what settings you put into what files and show how the .ear is structured?

                      Jeff Brooks


                      yeah i have the same problem. please be tell us how exactly you did that

                      • 8. Re: Persistence unit scoping across multiple jars in a singl
                        Nicolai Ødum Newbie

                        I have the same problem so a solution would be very nice.

                        "raja05" wrote:

                        from the EJB Spec,
                        Quote:

                        A persistence unit must have a name. Only one persistence unit of any given name may be defined within a single EJB-JAR file, within a single WAR file, within a single application client jar, or within an EAR (in the EAR root or lib directory).


                        But as understand it then JBoss 4.0.x will not support the new ear file layout - so does this work with the current version of JBoss?