With a composite persistence unit, you can:
Map relationships among any of the entities in multiple persistence units
Access entities stored in multiple databases and different data sources
Easily perform queries and transactions across the complete set of entities
Example 9-1 shows how you can persist data from a single persistence context into two different databases:
Example 9-1 Using Multiple Databases
em.persist(new A(..)); em.persist(new B(..)); // You can insert A into database1 and insert B into database2. // The two databases can be from different vendors. em.flush();
Figure 9-1 illustrates a simple composite persistence unit. EclipseLink processes the persistence.xml
file and detects the composite persistence unit, which contains two composite member persistence units:
Class A is mapped by a persistence unit named memberPu1 located in the member1.jar
file.
Class B is mapped by a persistence unit named memberPu2 located in the member2.jar
file.
Figure 9-1 A Simple Composite Persistence Unit
When using composite persistence units, note the following requirements:
The name of each composite member persistence unit must be unique within the composite.
The transaction-type
and other properties that correspond to the entire persistence unit (such as target server, logging, transactions, and so on) should be defined in the composite persistence unit. If not, the transaction types, target server information, and logging properties defined with composite members will be ignored.