5 Replies Latest reply on Mar 9, 2015 6:21 PM by Scott Marlow

    Global JPA model and Persistence unit

    Joan Pujol Espinar Newbie

      Is there a way to make a JPA Persistence Unit and model classes available globally for all the deployed projects on the server?


      We migrate from Jboss 4.2.3 where we used a global shared EntityManager with the PU inside (the model were in lib folder and the PU in lib/endorsed).

      And an ideal solution will be to reproduce that. Because we have 3 versions of the PU that only differs with the Hibernate Dialect and we have dozens of independent apps/ejbs that use the model.

      That way, in 4.2.3 we provide the needed JPA/PU jar globally per server (in function of the database installed)  and we don't need to make different versions of the other apps for database vendor that it's what we will have to do if we have to deploy he persistence.xml in each project.


      The obvious solution would be to include the JPA and persistence in a module, but that was asked in that thread and there isn't a solution.


      Any suggerence or workarround in sharing the JPA/PU between unrelated projects?


      EDIT: I've seen that there's an option to publish EntityManager and EntityManagerFactor to a global JNDI namespace.

      But it's not clear the usage pattern on that option. The idea is to publish it in one dummy EJB jar with the model that only publishes the EntityManager to a JNDI namespace?

      But where have to reside the JPA classes to be accessed globally from  another modules?

        • 1. Re: Global JPA model and Persistence unit
          Tomaž Cerar Master

          And yet again, response will be the same.


          create global module and make sure you create jandex indexes for it. and than when you are adding dependency to the global module make sure you also include indexes with it.

          see [AS7-1234] Hibernate does not read/use metadata for orm.xml defined entites - JBoss Issue Tracker and


          in short is it just matter of preparing your module properly and use it.

          • 2. Re: Global JPA model and Persistence unit
            Joan Pujol Espinar Newbie

            Thanks Tomaz. I will test it. Do you know if it's supported also to have the persistence.xml in the module?

            • 3. Re: Global JPA model and Persistence unit
              Scott Marlow Master

              Persistence.xml files are not deployed in global modules.  To share persistence units between deployments, internal service dependencies are needed on the persistence.  In other words, internal code changes are needed to accommodate this. 


              For example, if you have commonPU.jar deployed with the entity classes + persistence.xml, your other deployments that depend on commonPU.jar would have these dependencies set so that if commonPU.jar is redeployed, the other deployments would also restart.  There is also the sharing of the commonPU.jar classes that needs to work (including the enhancing/rewriting of entity classes).  The clustering of the JPA second level cache might also need work. 


              The global JNDI (jboss.entity.manager.factory.jndi.name/jboss.entity.manager.jndi.name) option that you mentioned, doesn't add the internal service dependency that I mentioned above.  So, if the deployment that contains the persistence.xml (with jboss.entity.manager.factory.jndi.name) is undeployed, the EntityManagerFactory will no longer be available in JNDI but dependent applications will not be aware.  The typical use of the global JNDI (jboss.entity.manager.factory.jndi.name/jboss.entity.manager.jndi.name) is for use within the same application (it does not setup the sharing of entity classes across deployments). 

              • 4. Re: Global JPA model and Persistence unit
                Joan Pujol Espinar Newbie

                Thanks Scott.

                When you say internal code changes are needed to accomodate this, do you mean WildFly internal code changes, don't you? Or in other words, that it's not supported at the moment, is it?

                • 5. Re: Global JPA model and Persistence unit
                  Scott Marlow Master

                  There is currently no way to setup the dependencies between deployments that you would need to share persistence units between deployments.  If your just sharing classes via a global module (like you asked about originally), you don't get to share a persistence unit but there are no dependencies issues (each deployment will need its own persistence.xml).