Two Persistence Unit in one Persistence.xml
Asked Answered
E

2

19

We created some libraries that all our projects will use, this libraries will provide the basic functionality of all our systems (login, some manage, etc). But the application itself could use another database.

What we did was to create the Persistence.xml with two persist units. And package all the core library entities in a jar called "LN-model.jar" and all of the entities of out test app in "App-model.jar". But for some reason we still obtain the following message.

Could not resolve a persistence unit corresponding to the persistence-context-ref-name [x.x.x.x.listener.InicializadorListener/em] in the scope of the module called [gfdeploy#/Users/zkropotkine/WORK/SeguridadCore/dist/gfdeploy/SeguridadCore-war_war]. Please verify your application.

Here's our Persistence.xml

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="x" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/x</jta-data-source>
    <jar-file>App-model.jar</jar-file>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    </properties> 
</persistence-unit>

<persistence-unit name="y" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/y</jta-data-source>
    <jar-file>LN-model.jar</jar-file>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
</persistence-unit> 

By the way we put the Persistence.xml in a jar, that we add to our Enterprise Project (EAR).

Edulcorate answered 18/3, 2011 at 18:14 Comment(0)
G
19

The problem is that the JPA does not know which is the persistence unit to use. when you have only one persistence unit this problem does not occur. To fix do the following:

You need to specify a persistence unit : @PersistenceContext(unitName="...") in the Ejb that do not have

Gusti answered 14/9, 2011 at 14:23 Comment(0)
V
12

You can add the annotations:

@PersistenceUnit(name = "x")
EntityManagerFactory entityManagerFactory;

@PersistenceContext(unitName = "y")
EntityManager entityManager;

Or you can create it manually:

EntityManagerFactory emfA = Persistence.createEntityManagerFactory("x", properties);
EntityManagerFactory emfB = Persistence.createEntityManagerFactory("y", properties);

For more details, please see the following link: https://docs.oracle.com/html/E25034_01/usingmultipledbs.htm is very useful, to me helped me!

Volumed answered 26/11, 2014 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.