Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/Examples/JPA/Composite

EclipseLink JPA: Composite Persistence Units Example

Starting with EclipseLink 2.3.0 JPA developers can now combine persistence units together at runtime allowing entities to be stored in different databases. This includes support for relationships between entities in different persistence units (references across databases). For long time users of TopLink/EclipseLink this feature is an upgrade to the native Session Broker functionality to make it easier to use with JPA.

Usage

  • Two or more persistence units could be combined into a single Composite persistence Unit.
  • Each composite members persistence unit keeps mapping its classes to its own database.
    • Therefore Composite persistence unit allows to map different entities to different data bases.

Basic Composition Example

Below is an example of Composite persistence unit definition. The persistence Unit 'compositePu' specifies transaction type and server platform. It contains all persistence units defined in member1.jar and member2.jar files.

Eclipselink composite-pu basic.png

<persistence 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 persistence_1_0.xsd" version="1.0">
    <persistence-unit name="compositePu" transaction-type="JTA">
        <provider>
            org.eclipse.persistence.jpa.PersistenceProvider
        </provider>
 
        <jar-file>member1.jar</jar-file>
        <jar-file>member2.jar</jar-file>
 
        <properties>
            <property name="eclipselink.composite-unit" value="true"/>
            <property name="eclipselink.target-server" value="WebLogic_10"/>
        </properties>
    </persistence-unit>
</persistence>
  • memberPu1 defined in member1.jar file. As it is a 'normal' persistence.xml file, and contains no relationships to other PUs, it can be used independently as well as inside composite persistence unit.
<persistence 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 persistence_1_0.xsd" version="1.0">
    <persistence-unit name="memberPu1" transaction-type="JTA">
        <provider>
            org.eclipse.persistence.jpa.PersistenceProvider
        </provider>
        <mapping-file>META-INF/advanced-entity-mappings1.xml</mapping-file>
        <jta-data-source>jdbc/OracleJtaDS</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.target-server" value="WebLogic_10"/>
            <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.oracle.Oracle11Platform"/>
        </properties>
    </persistence-unit>
</persistence>
  • memberPu2 defined in member2.jar file. As with memberPu1, it can be used independently as well as inside composite persistence unit.
<persistence 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 persistence_1_0.xsd" version="1.0">
    <persistence-unit name="memberPu1" transaction-type="JTA">
        <provider>
            org.eclipse.persistence.jpa.PersistenceProvider
        </provider>
        <mapping-file>META-INF/advanced-entity-mappings2.xml</mapping-file>
        <jta-data-source>jdbc/MySqlJtaDS</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.target-server" value="WebLogic_10"/>
            <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.oracle.MySqlPlatform"/>
        </properties>
    </persistence-unit>
</persistence>

For more information about Composite persistence units please see the design doc.

Relationship Example

Eclipselink composite-pu rel.png

Copyright © Eclipse Foundation, Inc. All Rights Reserved.