6

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
  • 1
    Why don't you put it in a dedicated jar file and all of your ejb modules depend on the additional jar? – mp911de Jun 05 '15 at 17:43
  • We need to package our EJB modules into two EARs which use different databases. The best way for us would be to put persistence.xml in the EAR. – Evandro Pomatti Jun 05 '15 at 18:02

2 Answers2

4

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

Community
  • 1
  • 1
Cris
  • 4,947
  • 6
  • 44
  • 73
  • Since my contribution to this answer is based on others response would not like to get the bounty in case it helps you. – Cris Jun 07 '15 at 20:51
  • Using JPA 2.0 didn't solve problem. It turns out that I need to deploy a JAR under the /lib directory. Uploading an answer soon. – Evandro Pomatti Jun 08 '15 at 16:12
3

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.

Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
  • Yes...if you look on the second solution provided EAR + |- lib + | |- core-module.jar | \- persistence-module.jar + | \- META-INF + | \- persistence.xml |- ejb1-module.jar \- ejb2-module.jar – Cris Jun 08 '15 at 19:15