Question

Supposing I have a jar with the persistence.xml configuration and the jpa entities. I would like to have the same persistence unit shared among different applications.

For example deploy a war application and have the same persistence unit (that other applications use) injected.

@PersistenceContext(unitName="MySharedPersistence")
private EntityManager entityManager;    

I can not consider packaging all the applications in an ear file since I want to have the other applications up and running while I reupload an application (the application reuploaded uses some remote ejbs from the already deployed applications and uses the same persistence unti as described above).

Are there any solutions to this problem?

Thank you in advance.

EDIT: Probably It is not recomended are there any appropriate tryouts with the same result?

Was it helpful?

Solution

You can place your Entity classes and your persistence.xml in a jar, and reuse this jar in all projects. As long as you also include a beans.xml file along with your persistence.xml, it should work just fine. You should then be able to inject the persistence context in any project that uses this jar file.

You need the beans.xml file for autodiscovery by the container. For reference, here's how a beans.xml file would look:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top