Contents

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. This example assumes two persistence units composed into a single parent unit. In this simple case, there are no relationships between the two units.

The parent Persistence Unit 'compositePu' specifies transaction type and server platform, and contains a property to define it as the parent PU. It 'contains' all persistence units defined in member1.jar and member2.jar files.

memberPu1 defined in member1.jar file. As it contains no relationships to other PUs, it can be used independently as well as inside composite persistence unit. (Note, this pu is mapped to an Oracle DB)

Relationship Example

For this example, take the simple example above and add a relationship from a class in Pu1, to a class in Pu2.

The persistence xml files for 'compositePu' and 'memberPu2', stay exactly the same. However, memberPu1, needs a flag to indicate that it has a relationship to another pu. This also adds the restriction that memberPu1 can not be used as it's own independent persistence unit.