Multiple persistence units in persistence.xml using EclipseLink

Here one example about how to configure the persistence using JPA, EclipseLink, Glassfish, ApacheDerby and Netbeans
- I have a working app with the following persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="https://java.sun.com/xml/ns/persistence" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://java.sun.com/xml/ns/persistence https://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="marketsPu" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/markets</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<property name="eclipselink.jdbc.batch-writing.size" value="10000"/>
</properties>
</persistence-unit>
</persistence>
- I had to create a second persistence unit in persistence.xml to connect to a second database.
<persistence-unit name="marketsRT" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/marketsRT</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<property name="eclipselink.jdbc.batch-writing.size" value="10000"/>
</properties>
When I rebuilt the app I add the following error:
java.lang.RuntimeException: javax.annotation.processing.FilerException: Attempt to recreate a file for type
- to solve the issue I had to define for each persistence unit his own classes and change
<exclude-unlisted-classes>
to false:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="https://java.sun.com/xml/ns/persistence" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://java.sun.com/xml/ns/persistence https://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="marketsPu" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/markets</jta-data-source>
<class>ch.genidea.genimarkets.entity.ClassBlaBla</class>
<class>...<class>
...
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<property name="eclipselink.jdbc.batch-writing.size" value="10000"/>
</properties>
- I rebuilt and I got again the same error.
adding for the first persistence unit :
<property name="eclipselink.canonicalmodel.subpackage" value="one"/>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<property name="eclipselink.jdbc.batch-writing.size" value="10000"/>
<property name="eclipselink.canonicalmodel.subpackage" value="one"/>
</properties>
and for the second persistence unit :
<property name="eclipselink.canonicalmodel.subpackage" value="two"/>
seemed to solve the problem.
During the deploy I had the following error:
SEVERE: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [service class name] ...
for glassfish was not possible to decide which is the correct persistence-unit to use in the service layer.
The solution is :
- declare a
<persistence-context-ref>
in web.xml
<persistence-context-ref>
<persistence-context-ref-name>persistence/markets</persistence-context-ref-name>
<persistence-unit-name>marketsPu</persistence-unit-name>
</persistence-context-ref>
<persistence-context-ref>
<persistence-context-ref-name>persistence/marketsRT</persistence-context-ref-name>
<persistence-unit-name>marketsRT</persistence-unit-name>
</persistence-context-ref>
- explicitly declare the name of the persistence context in the classes that uses EntityManager
@PersistenceContext(name="persistence/markets", unitName="marketsPu")
EntityManager em;
@PersistenceContext(name="persistence/marketsRT", unitName="marketsRT")
EntityManager em;
You can find more informations here: