Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share same JPA persistence.xml across multiple EJB JARs inside EAR

Tags:

java

jpa

weblogic

Our project has multiple EJB modules and we want to share a single persistence.xml file between them.

We put the persistence.xml file inside EARs META-INF directory, but the persistence unit is not available at runtime. It seems like the file is never read since we forced incorrect classes and jar files, but nothing happens.

Why is WebLogic not reading the persistence.xml file inside the EAR?

We get the following error when running the code, no PU is found (Available persistence units: []).

Caused By: java.lang.IllegalArgumentException: No persistence unit named 'em' is available in scope ejb1-module.jar. Available persistence units: []

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="system-unit" transaction-type="JTA">
        <jta-data-source>sysmtem-ds</jta-data-source>
        <jar-file>../ejb1-module.jar</jar-file>
        <class>...</class>
        <class>...</class>
        <class>...</class>
    </persistence-unit>
</persistence>

Structure (persistence unit is placed inside the EAR)

EAR +
    |- META-INF +
    |       - persistence.xml
    |- ejb1-module.jar
    |- ejb2-module.jar
    |- ejb3-module.jar

We are using WebLogic 10.3.6 which uses JPA 1.0 and TopLink/EclipseLink that shipts with it.

like image 921
Evandro Pomatti Avatar asked Jun 05 '15 16:06

Evandro Pomatti


2 Answers

You should upgrade to JPA 2.0

See this link : JPA 2.0 in WebLogic Server 10.3.6

Then follow the answer from

Sharing a persistence unit across components in a .ear file

like image 199
Cris Avatar answered Oct 13 '22 00:10

Cris


Deploying the persistence.xml under the EARs META-INF folder will not trigger the server to load the persistence unit (at least not in WebLogic 10g, even with JPA 2.0 enabled).

The persistence unit needs to be deployed under /lib using a JAR file. Working structure:

EAR +
    |- lib +
    |      |- entities.jar
    |      \- persistence-unit.jar +
    |                               \- META-INF +
    |                                            \- persistence.xml
    |- ejb-core-module.jar
    \- ejb-business-module.jar

In this example the "persistence-unit.jar" is used to pack the JPA configurations.

Now I am able to create another EARs using different data sources. Using composite persistence units might increase the reusability of this approach as well.

like image 35
Evandro Pomatti Avatar answered Oct 12 '22 23:10

Evandro Pomatti