Multiple persistence units in persistence.xml using EclipseLink


Here one example about how to configure the persistence using JPA, EclipseLink, Glassfish, ApacheDerby and Netbeans

  1. 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> 
  1. 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 
  1. 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> 
  1. 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 :

  1. 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> 
  1. 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:

I can make it

Oracle



WebApp built by Marco using SpringBoot, Java 17, Mustache, Markdown and in Azure