0 Replies Latest reply on Mar 11, 2010 6:14 AM by thomasgo

    Define injected persistence unit outside the ejb.jar

    thomasgo

      Hi,

       

      I have a question regarding the injection of persistence units/entity managers:

       

      Consider the following setup:

       

      ear

      +- ejb_a.jar

        |  +- DAO_A

        |  +- Entity_A

      +- ejb_b.jar

        |  +- DAO_B

        |  +- Entity_B

      +- ejb_c.jar

        |  +- DAO_C

        |  +- Entity_C

      +- persistence.jar

           +- persistence.xml

           +- orm.xml

       

      ejb_a.jar and ejb_b.jar are components that are independent of the application. However, since the application might require some specific setup, the components only define some default settings via annotations, e.g. @Table(...) etc.

       

      The project-specific persistence.jar contains a persistence.xml that references the orm.xml which in turn defines application specific overrides.

      This works so far.

       

      The problem I'm facing now is the following:

       

      Each of the DAOs currently references a persistence unit by name (@PersistenceContext(unitName = "XXX")), which is defined in the persistence.xml. If DAO_B needs to load instances of Entity_A it should use the same persistence unit.

       

      However, if Entity_A and Entity_B are independent and sometimes need to be stored in different databases, I need to specify different persistence units. If I now have a case where Entity_C and DAO_C need access to both Entity_A and Entity_B (in which case they can't be in different databases) I'd need to create another persistence unit which references all 3 entities (resp. all 3 jars via the <jar-file> tag). Since the jar's generally are independent components, changing the unitName in the source isn't an option.

       

      This now would lead to 3 persistence units being defined, PU_A, PU_B and PU_C.

      Each of those reference one or more jars with PU_C referencing all 3 whereas PU_A and PU_B just reference ejb_a.jar/ejb_b.jar.

       

      If you extend that example you can see that there would be an explosion of persistence units that are required.

       

       

      So finally, here's the actual question:

       

      I'd like to inject the persistence unit into the DAOs via a deployment descriptor external to the ejb.jar.

      AFAIK ejb-jar.xml isn't an option, since it needs to be included in the jar file, doesn't it?

       

      The next thing I found was <persistence-context-ref> which seemed like a viable option: define a persistence context with a given name for the DAO and map that context to a persistence unit externally. The mapping needs to be put in a deployment descriptor, so application.xml seems to be fine.

       

      However, this didn't work, with JBoss 4.2.3.GA complaining about no default persistence unit being found (which indicates the mapping is either ignored or wrong).

       

      Another thing I found is that most sites on the net give examples for putting that mapping inside the servlet definition in web.xml, so it might only work for non-ejb classes. Is that true?

       

      Thanks for reading my quite lengthy post and even more thanks for your answers.

       

      Regards,

       

      Thomas