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

Difference between revisions of "EclipseLink/UserGuide/sandbox/gelernter/Composite Persistence Units"

m
(Limitations)
 
(26 intermediate revisions by 2 users not shown)
Line 6: Line 6:
 
|api=y
 
|api=y
 
|apis=
 
|apis=
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/VirtualAccessMethods.html @VirtualAccessMethods]
+
* [[http://www.eclipse.org/eclipselink/api/2.3/org/eclipse/persistence/config/EntityManagerProperties.html EntityManagerProperties]]
 
|examples=y
 
|examples=y
 
|example=
 
|example=
*[[EclipseLink/Examples/JPA/Extensibility|Extensible Entities]]
+
*[[http://wiki.eclipse.org/EclipseLink/Examples/JPA/Composite Composite Persistence Units Example]]
 
}}
 
}}
===******SANDBOX VERSION******===
 
  
 
= Composite Persistence Units=
 
= Composite Persistence Units=
Line 20: Line 19:
 
<br>
 
<br>
  
Multiple persistence units with unique sets of entity types can be exposed as a single persistence context by using a ''composite persistence unit''. The single context can access entities stored in different data sources, so mapping relationships can be established between them, and queries and transactions can be performed transparently across the complete set of entities.  
+
Multiple persistence units with unique sets of entity types can be exposed as a single persistence context by using a ''composite persistence unit''. A persistent unit that is part of a composite persistent unit is called a ''composite member persistent unit''.
 +
 
 +
With composite persistence units:
 +
* Mapping relationships can be established among any of the entities in the composite.
 +
* The persistence context can access entities stored in different data sources.
 +
* Queries and transactions can be performed transparently across the complete set of entities.  
  
 
For example, you could persist data from a single context into different data sources, as shown below:
 
For example, you could persist data from a single context into different data sources, as shown below:
Line 27: Line 31:
 
em.persist(new A(..));
 
em.persist(new A(..));
 
em.persist(new Z(..));
 
em.persist(new Z(..));
// insert A into db1; insert Z into db2:
+
// Insert A into database1; insert Z into database2:
// the two different databases can be from different vendors
+
// the two databases can be from different vendors.
 
em.flush();
 
em.flush();
 
</source>
 
</source>
  
A persistent unit that is part of a composite persistent unit is called a ''composite member persistent unit''.
 
  
{{EclipseLink_Note
+
==Configuring Composite Persistence and Composite Member Persistence Units ==
|note= Joins across tables in different data sources are not supported. This limitation affects mapping and query execution and optimizations.
+
}}
+
  
==Configuring Composite Persistence Units ==
+
Both composite persistence units and their member persistence units are regular persistence units, each of them must have persistence.xml.
  
You can configure composite persistence units in <tt>persistence.xml</tt> or in the <tt>EntityManagerFactory</tt>.
+
===Configuring Composite Persistent Units in persistence.xml ===
  
===Configuring persistence.xml ===
+
* Use the <tt><property name="eclipselink.composite-unit" value="true"/></tt> property identify persistence unit as a composite. ('''Note:''' If this property is passed to the <tt>createEntityManagerFactory</tt> method or if it is set in system properties, it is ignored.)
  
Configure a composite persistent unit in <tt>persistence.xml</tt>, as follows:
+
* Use the <tt><jar-file></tt> element to specify the jar files containing the composite member persistent units. The composite persistence unit will contain all the persistence units found in the jar files specified.  
* Use the <tt>eclipselink.composite-unit</tt> property to indicate that it is a composite persistence unit. This property passed to the <tt>createEntityManagerFactory</tt> method or in system properties is ignored'''''////?/////'''''.
+
* Use <tt><jar-file></tt> element to specify the jar files containing the composite member persistent units. The composite persistence unit will contain all the persistence units found in the jar files specified. For example,
+
 
+
<source lang="xml">
+
...
+
<jar-file>member1.jar</jar-file>
+
<jar-file>member2.jar</jar-file>
+
<properties>
+
    <property name="eclipselink.composite-unit" value="true"/>
+
</properties>
+
...
+
</source>
+
 
+
Use the <tt>eclipselink.composite-unit.member</tt> property to specify whether or not a persistent unit should be a member of a composite persistence unit:
+
* <tt>true</tt> specifies that the persistent unit ''must'' be a member of a composite persistence unit.
+
* <tt>false</tt> specifies that the persistent unit ''cannot'' be a member of a composite persistence unit.
+
 
+
For example,
+
 
+
<source lang="xml">
+
...
+
<properties>
+
    <property name="eclipselink.composite-unit.member" value="true"/>
+
</properties>
+
...
+
</source>
+
 
+
=== Passing Properties to EntityManagerFactory ===
+
 
+
The property passed to the <tt>createEntityManagerFactory</tt> method or in system properties is ignored.
+
 
+
{{EclipseLink_Note
+
|note=If this property is set to <tt>true</tt>, the <tt>EntityManagerFactory</tt> can still be created, but it cannot be connected.}}
+
 
+
=== Passing EntityManagerFactory Properties to Composite Member Persistent Units ===
+
While creating a composite persistent unit, <tt>EntityManagerFactory</tt> properties can be passed to composite member persistent units, as shown in the following example. * The value is a map, the key is a member persistence unit's name,
+
* the value is a map of properties to be passed to this persistence unit.
+
 
+
 
+
<source lang="java">
+
"eclipselink.composite-unit.properties" -> (
+
  ("memberPu1" -> (  "javax.persistence.jdbc.user" -> "user1",
+
                          "javax.persistence.jdbc.password" -> "password1",
+
                          "javax.persistence.jdbc.driver" -> "oracle.jdbc.OracleDriver",
+
                          "javax.persistence.jdbc.url" -> "jdbc:oracle:thin:@oracle_db_url:1521:db",
+
                    ) ,
+
  ("memberPu2" -> (  "javax.persistence.jdbc.user" -> "user2",
+
                          "javax.persistence.jdbc.password" -> "password2"
+
                          "javax.persistence.jdbc.driver" -> "com.mysql.jdbc.Driver",
+
                          "javax.persistence.jdbc.url" -> "jdbc:mysql://my_sql_db_url:3306/user2",
+
                    )
+
</source>
+
  
* The transaction type should be specified in the composite persistence unit. Transaction types of composite members are ignored.
+
* The transaction type may be specified in the composite persistence unit (otherwise default transaction type is used). Transaction types of composite members are ignored.
* Data sources should be specified in composite member persistence units. Those specified in the composite are ignored.
+
* Some persistence unit properties apply to the composite persistence unit (like "eclipselink.target-server") and may be specified in composite pu persistence.xml (these properties ignored if specified in composite members). For complete list of these properties see the table ...
 +
* Don't specify any classes in composite persistence unit - all the classes should be specified in composite members.
  
=== Example ===
+
====Example====
In the following example, the composite persistence unit <tt>compositePU</tt> specifies the transaction type and the server platform. It will contain all persistence units defined in <tt>member1.jar</tt> and <tt>member2.jar</tt> files.
+
The composite persistence unit <tt>compositePU</tt> specifies the transaction type and the server platform. It will contain all persistence units defined in <tt>member1.jar</tt> and <tt>member2.jar</tt> files.
  
 
<source lang="xml">
 
<source lang="xml">
Line 122: Line 72:
 
</source>
 
</source>
  
In the following example, the composite member persistence unit <tt>memberPu1</tt> is defined in the <tt>member1.jar</tt> file. It could be used independently as well as inside composite persistence unit.
+
===Configuring Composite Member Persistent Units in persistence.xml ===
 +
* Data sources may be specified in composite member persistence units.  Those specified in the composite are ignored.
 +
* Some persistence unit properties apply to the composite persistence unit member only (like "eclipselink.target-database") and may be specified in composite members pu persistence.xml (these properties ignored if specified in the composite pu). For complete list of these properties see the table ...
 +
* Normally composite member pu could also be used as stand alone pu (independent from the composite). However if a member pu has dependencies of another member(s) (for instance a class defined in memberPu2 has a reference to a class defined in memberPu1) then such pu can no longer be used as a stand alone - only as a member of the composite.
 +
** To indicate that a member pu ''must'' be a member of a composite pu <property name="eclipselink.composite-unit.member" value="true"/> should be specified in its persistence.xml. ('''Note:''' If this property is passed to the <tt>createEntityManagerFactory</tt> method or if it is set in system properties, it is ignored.)
 +
*** Attempt to create an EntityManagerFactory for such pu will still succeed (that's required by application servers), however attempt to create an EntityManager from the factory would fail
  
 +
====Example====
 +
Composite member persistence unit <tt>memberPu1</tt> is defined in the <tt>member1.jar</tt> file. It can be used independently as well as inside composite persistence unit.
 +
Note that transaction type and server platform will be ignored if memberPu1 used as a composite member pu, but they will be used if memberPu1 is used stand alone (as a regular persistence unit).
 
<source lang="xml">
 
<source lang="xml">
 
<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 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">
Line 141: Line 99:
 
</source>
 
</source>
  
In the following example, the composite member persistence unit <tt>memberPu2</tt> is defined in the <tt>member2.jar</tt> file. It has dependency on a class defined in member1.jar and can't be used independently.
+
Composite member persistence unit <tt>memberPu2</tt> is defined in the <tt>member2.jar</tt> file. It has dependency on a class defined in member1.jar and cannot be used independently.
  
 
<source lang="xml">
 
<source lang="xml">
Line 159: Line 117:
 
</persistence>
 
</persistence>
 
</source>
 
</source>
 +
 +
<!---//REVIEWERS: Design doc also says "If this property is set to true, the EntityManagerFactory can still be created, but it cannot be connected." I don't understand, as true is perfectly valid, no?// //ANDREI: true is perfectly valid. That means persistence unit can only be used as a member of composite - cannot be used as an independent persistence unit. Motivation: persistence unit that has dependency on classes in other persistence unit(s) - impossible to use standalone, still possible to use as a member of composite, of course the persistence unit(s) on which it depends should also be members in the same composite. Logically I would prefer to fail when the factory is created - but that would be a problem with application servers, so the next best solution is to fail on attempt to actually use the factory - createEntityManager// -->
 +
=== Passing Persistence Unit Properties to Composite Member Persistent Units ===
 +
As with regular persistence units, you can override properties specified in <tt>persistence.xml</tt> with new ones passed to the <tt>createEntityManagerFactory</tt> method. Therefore, you can change some properties at runtime, for example connection parameters. To do this with composite member persistence units, use the <tt>eclipselink.composite-unit.properties</tt> property. The value is a <tt>Map</tt> that is keyed on composite member persistence unit names; the value is a <tt>Map</tt> that contains all properties to be passed to this member persistence unit. For example,
 +
 +
<source lang="java">
 +
Map props1 = new HashMap();
 +
 +
  props1.put(PersistenceUnitProperties.JDBC_USER, "user1");
 +
  props1.put(PersistenceUnitProperties.JDBC_PASSWORD, "password1");
 +
  props1.put(PersistenceUnitProperties.JDBC_DRIVER, "oracle.jdbc.OracleDriver");
 +
  props1.put(PersistenceUnitProperties.JDBC_URL, "jdbc:oracle:thin:@oracle_db_url:1521:db");
 +
 +
Map props2 = new HashMap();
 +
 +
  props2.put(PersistenceUnitProperties.JDBC_USER, "user2");
 +
  props2.put(PersistenceUnitProperties.JDBC_PASSWORD, "password2");
 +
  props2.put(PersistenceUnitProperties.JDBC_DRIVER, "oracle.mysql.OracleDriver");
 +
  props2.put(PersistenceUnitProperties.JDBC_URL, " jdbc:mysql:@oracle_sql_url:3306/user2");
 +
 +
Map memberProps = new HashMap();
 +
  memberProps.put("memberPu1", props1);
 +
  memberProps.put("memberPu2", props2);
 +
 +
Map props = new HashMap();
 +
  props.put("eclipselink.composite-unit.properties", memberProps);
 +
 
 +
EntityManagerFactory emf = Persistence.createEntityManagerFactory("composite", props);
 +
 +
</source>
 +
 +
Only persistence unit properties that are processed by composite members (as indicated in the [[#Persistence Unit Properties| Persistence Unit Properties]] table, below) should be passed to a member. Other properties are ignored. The same rule applies for the <tt>createEntityManager</tt> method; only those entity manager properties that are processed on members (as indeicated in the [[#Entity Manager Properties| Entity Manager Properties]] table) should be passed. Other properties are ignored.
  
 
==Deploying and Accessing a Composite Persistence Unit==
 
==Deploying and Accessing a Composite Persistence Unit==
Line 175: Line 165:
 
</source>
 
</source>
  
* Or it can be created manually, using  [[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Persistence.html <tt>Persistence</tt>]], as shown in the following example:
+
* Or it can be created manually, using  [[http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Persistence.html <tt>Persistence.createEntityManagerFactory </tt>]], as shown in the following example:
  
:<source lang="java">
+
<source lang="java">
 
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("compositePu", properties);
 
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("compositePu", properties);
 
</source>
 
</source>
  
== Persistence Unit Properties ==
+
== Persistence Unit Properties Set on the Entity Manager Factory==
 +
 
 +
Persistence unit properties can be set on the <tt>EntityManagerFactory</tt> as follows:
 +
 
 +
* Composite persistence unit properties can be specified either in the composite persistence unit's <tt>persistence.xml</tt> file or passed to the its <tt>createEntityManagerFactory()</tt> method.
 +
* Composite member persistence unit properties can either be specified in the composite member persistence unit's <tt>persistence.xml</tt> file or passed to the composite persistence unit's <tt>createEntityManagerFactory()</tt> method through the <tt>eclipselink.composite.properties</tt> property.
 +
 
 +
The following tables show where to set properties: on the composite persistence unit or on the composite member persistence unit. You should only pass properties to a composite or to a member that can be processed by the composite or the member. That is,
 +
 
 +
*Only pass to a composite persistence unit those properties that are processed on composite persistence units. All other properties are ignored.
 +
*Only pass to a composite persistence unit member (through <tt>eclipselink.composite.properties</tt>) the properties that are processed on composite member persistence units. All other properties are ignored. 
 +
 
 +
For complete <tt>PersistenceUnitProperties</tt> reference documentation, see [[http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/config/PersistenceUnitProperties.html |PersistenceUnitProperties]].
 +
 
 +
===Persistence Unit Properties That Can Be Set on Composite Persistence Units===
 +
 
 +
You can set the following persistence unit properties on composite persistence units. All other properties will be ignored.
  
* Composite Persistence Unit property could be specified either in its persistence.xml or passed to createEntityManagerFactory method;
 
* Composite member Persistence Unit property could be either specified in its persistence.xml or passed to composite's createEntityManagerFactory through "eclipselink.composite.properties" property.
 
 
{|{{BMTableStyle}}
 
{|{{BMTableStyle}}
 +
|-{{BMTableCaptionStyle
 +
|caption=Persistence Unit Properties Set on Composite Persistence Units}}
 
|-{{BMTHStyle}}
 
|-{{BMTHStyle}}
 
| PersistenceUnitProperties.
 
| PersistenceUnitProperties.
| property name
+
| Property Name
| composite pu
+
| composite member pu
+
 
| comment
 
| comment
 
|-  
 
|-  
 
| COMPOSITE_UNIT
 
| COMPOSITE_UNIT
 
| eclipselink.composite-unit
 
| eclipselink.composite-unit
| processed
+
| Must be specified in persistence.xml of a composite persistence unit with the value "true". Advanced usage: This property can also be set on a composite member persistence unit that is itself a composite persistence unit. In that case, the composite is substituted by its own members.
| advanced
+
| if specified by member then the member is substituted for its own members
+
|-
+
| COMPOSITE_UNIT_MEMBER
+
| eclipselink.composite-unit.member
+
| advanced
+
| processed
+
| if specified by composite then the composite must be member of another composite
+
 
|-  
 
|-  
 
| COMPOSITE_PROPERTIES
 
| COMPOSITE_PROPERTIES
 
| eclipselink.composite.properties
 
| eclipselink.composite.properties
| processed
 
| ignored
 
 
|  
 
|  
 
|-  
 
|-  
 
| TRANSACTION_TYPE
 
| TRANSACTION_TYPE
 
| javax.persistence.transactionType
 
| javax.persistence.transactionType
| processed
 
| ignored
 
 
|  
 
|  
 
|-  
 
|-  
 
| TARGET_SERVER
 
| TARGET_SERVER
 
| eclipselink.target-server
 
| eclipselink.target-server
| processed
 
| ignored
 
 
|  
 
|  
 
|-  
 
|-  
 
| LOGGING_*
 
| LOGGING_*
 
| eclipselink.logging.*
 
| eclipselink.logging.*
| processed
 
| ignored
 
 
|  
 
|  
 
|-  
 
|-  
 
| PROFILER
 
| PROFILER
 
| eclipselink.profiler
 
| eclipselink.profiler
| processed
 
| ignored
 
 
|  
 
|  
 
|-  
 
|-  
 
| COORDINATION_*
 
| COORDINATION_*
 
| eclipselink.cache.coordination.*
 
| eclipselink.cache.coordination.*
| processed
 
| ignored
 
 
|  
 
|  
 
|-  
 
|-  
 
| SESSION_NAME
 
| SESSION_NAME
 
| eclipselink.session-name
 
| eclipselink.session-name
| processed
+
| This property can also be set on a composite member persistence unit . The member session name is used to read the session from <tt>sessions.xml</tt>, but the deployed session always has the same name as the member persistence unit (as it is defined in <tt>memberPuInfo.getPersistenceUnitName()</tt>)
| advanced
+
| member session name is used to read the session from sessions.xml, but the deployed session always has the same name as the member persistence unit (as it's defined in memberPuInfo.getPersistenceUnitName())
+
 
|-  
 
|-  
 
| DEPLOY_ON_STARTUP
 
| DEPLOY_ON_STARTUP
 
| eclipselink.deploy-on-startup
 
| eclipselink.deploy-on-startup
| processed
 
| ignored
 
 
|  
 
|  
 
|-  
 
|-  
 
| VALIDATION_ONLY_PROPERTY
 
| VALIDATION_ONLY_PROPERTY
 
| eclipselink.validation-only
 
| eclipselink.validation-only
| processed
+
| In 2.3: This property is also processed if set on a composite member persistence unit, but that is a known bug ({{bug|348815}}). This property '''should''' be processed by the composite only and ignored by member persistence units. To work around this bug, specify the property in the composite and in all its members. THIS IS FIXED IN 2.3.1: it's ignored by members.
| ''processed''
+
| {{bug|348815}}: WRONG BEHAVIOUR, should be processed by composite only, ignored by member persistence unit. Workaround for now: specify the property in the composite and in all its members.
+
 
|-  
 
|-  
 
| VALIDATION_*
 
| VALIDATION_*
 
| eclipselink.validation.*
 
| eclipselink.validation.*
| processed
 
| ignored
 
 
|
 
|
 
|-  
 
|-  
 
| CLASSLOADER
 
| CLASSLOADER
 
| eclipselink.classloader
 
| eclipselink.classloader
| processed
 
| ignored
 
 
|
 
|
 
|-
 
|-
 
| THROW_EXCEPTIONS
 
| THROW_EXCEPTIONS
 
| eclipselink.orm.throw.exceptions
 
| eclipselink.orm.throw.exceptions
| processed
 
| ignored
 
 
|  
 
|  
 
|-
 
|-
 
| FLUSH_CLEAR_CACHE
 
| FLUSH_CLEAR_CACHE
 
| eclipselink.flush-clear.cache
 
| eclipselink.flush-clear.cache
| processed
 
| ignored
 
 
|  
 
|  
 
|-
 
|-
 
| VALIDATE_EXISTENCE
 
| VALIDATE_EXISTENCE
 
| eclipselink.validate-existence
 
| eclipselink.validate-existence
| processed
 
| ignored
 
 
|  
 
|  
 
|-
 
|-
 
| JOIN_EXISTING_TRANSACTION
 
| JOIN_EXISTING_TRANSACTION
 
| eclipselink.transaction.join-existing
 
| eclipselink.transaction.join-existing
| processed
 
| ignored
 
 
|  
 
|  
 
|-
 
|-
 
| PERSISTENCE_CONTEXT_*
 
| PERSISTENCE_CONTEXT_*
 
| eclipselink.persistence-context.*
 
| eclipselink.persistence-context.*
| processed
 
| ignored
 
 
|  
 
|  
 
|-
 
|-
 
| ALLOW_ZERO_ID
 
| ALLOW_ZERO_ID
 
| eclipselink.allow-zero-id
 
| eclipselink.allow-zero-id
| processed
 
| ignored
 
 
|  
 
|  
 
|-
 
|-
 
| SESSION_CUSTOMIZER
 
| SESSION_CUSTOMIZER
 
| eclipselink.session.customizer
 
| eclipselink.session.customizer
| processed
+
| This property can also be set on a composite member persistence unit. Customizers of member persistence units are processed before the composite persistence unit's customizer.
| processed
+
|-
| Members' customizers processed before composite customizer.
+
| SESSION_EVENT_LISTENER_CLASS
 +
| eclipselink.session-event-listener
 +
|This property can also be set on a composite member persistence unit. An
 +
<tt>EventListener</tt> defined by a composite member will not receive events raised by a <tt>UnitOfWork</tt>; it will only receive events raised by its member. Also see {{bug|348766}} - FIXED in 2.3.1. An <tt>EventListener<>/tt> specified by a composite will receive all events (raised by either a unit of work or by members).
 +
|-
 +
| Exception_HANDLER_CLASS
 +
| eclipselink.exception-handler
 +
| This property can also be set on a composite member persistence unit. The first <tt>ExceptionHandler</tt> specified by a member handles the exception. If it is not specified or fails, then the composite's <tt>ExceptionHandler</tt> handles the original exception.
 +
|-
 +
| WEAVING
 +
| eclipselink.weaving
 +
| A composite persistent unit switches weaving on and off for all its member persistent units.
 +
|-
 +
|}
 +
 
 +
===Persistence Unit Properties That Can Be Set on Composite Member Persistence Units===
 +
 
 +
You can set the following persistence unit properties on composite member persistence units. All other properties will be ignored.
 +
 
 +
{|{{BMTableStyle}}
 +
|-{{BMTableCaptionStyle
 +
|caption=Persistence Unit Properties Set on Composite Member Persistence Units}}
 +
|-{{BMTHStyle}}
 +
| PersistenceUnitProperties.
 +
| Property Name
 +
| comment
 +
|-
 +
| COMPOSITE_UNIT
 +
| eclipselink.composite-unit
 +
| Advanced usage: This property can also be set on a composite member persistence unit that is itself a composite persistence unit. In that case, the composite is substituted by its own members. 
 +
|-
 +
| COMPOSITE_UNIT_MEMBER
 +
| eclipselink.composite-unit.member
 +
|The property should be specified with a value "true" in persistence.xml to indicate that the persistence unit cannot be used stand alone (only as a member of a composite).
 +
|-
 +
| SESSION_NAME
 +
| eclipselink.session-name
 +
| This property can also be set on a composite persistence unit. The member session name is used to read the session from <tt>sessions.xml</tt>, but the deployed session always has the same name as the member persistence unit (as it is defined in <tt>memberPuInfo.getPersistenceUnitName()</tt>)
 +
|-
 +
| VALIDATION_ONLY_PROPERTY
 +
| eclipselink.validation-only
 +
| This property is processed if set on a composite member persistence unit, but that is a known bug ({{bug|348815}}). This property '''should''' be processed by the composite only and ignored by member persistence units. To work around this bug, specify the property in the composite and in all its members. FIXED in 2.3.1 - ignored on member.
 +
|-
 +
| VALIDATION_*
 +
| eclipselink.validation.*
 +
|
 +
|-
 +
| CLASSLOADER
 +
| eclipselink.classloader
 +
|
 +
|-
 +
| THROW_EXCEPTIONS
 +
| eclipselink.orm.throw.exceptions
 +
|
 +
|-
 +
| FLUSH_CLEAR_CACHE
 +
| eclipselink.flush-clear.cache
 +
|
 +
|-
 +
| VALIDATE_EXISTENCE
 +
| eclipselink.validate-existence
 +
|
 +
|-
 +
| JOIN_EXISTING_TRANSACTION
 +
| eclipselink.transaction.join-existing
 +
|
 +
|-
 +
| PERSISTENCE_CONTEXT_*
 +
| eclipselink.persistence-context.*
 +
|
 +
|-
 +
| ALLOW_ZERO_ID
 +
| eclipselink.allow-zero-id
 +
|
 +
|-
 +
| SESSION_CUSTOMIZER
 +
| eclipselink.session.customizer
 +
| This property can also be set on a composite persistence unit. Customizers of member persistence units are processed before the composite persistence unit's customizer.
 
|-
 
|-
 
| SESSIONS_XML
 
| SESSIONS_XML
 
| eclipselink.sessions-xml
 
| eclipselink.sessions-xml
| exception
+
|If this property is set on a composite persistence unit, an exception will be thrown. Composite persitence units cannot be read from <tt>sessions.xml</tt>. Note that a non-composite persistence unit should be able to use a <tt>SessionBroker</tt> defined in <tt>sessions.xml</tt>.
| processed
+
| Composite can't be read from sessions.xml - exception will be thrown. Note that a non-composite persistence unit should be able to use a SessionBroker defined in sessions.xml (not tested, but should work).
+
 
|-
 
|-
 
| JTA_DATASOURCE
 
| JTA_DATASOURCE
 
| javax.persistence.jtaDataSource
 
| javax.persistence.jtaDataSource
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| NON_JTA_DATASOURCE
 
| NON_JTA_DATASOURCE
 
| javax.persistence.nonJtaDataSource
 
| javax.persistence.nonJtaDataSource
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| NATIVE_SQL
 
| NATIVE_SQL
 
| eclipselink.jdbc.native-sql
 
| eclipselink.jdbc.native-sql
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| SQL_CAST
 
| SQL_CAST
 
| eclipselink.jdbc.sql-cast
 
| eclipselink.jdbc.sql-cast
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| JDBC_*
 
| JDBC_*
 
| javax.persistence.jdbc.* eclipselink.jdbc.*
 
| javax.persistence.jdbc.* eclipselink.jdbc.*
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| CONNECTION_POOL_*
 
| CONNECTION_POOL_*
 
|  
 
|  
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| PARTITIONING.*
 
| PARTITIONING.*
 
| eclipselink.partitioning.*
 
| eclipselink.partitioning.*
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| EXCLUSIVE_CONNECTION_*
 
| EXCLUSIVE_CONNECTION_*
 
| eclipselink.jdbc.exclusive-connection.*
 
| eclipselink.jdbc.exclusive-connection.*
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| CACHE.*
 
| CACHE.*
 
| eclipselink.cache.*
 
| eclipselink.cache.*
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| TARGET_DATABASE
 
| TARGET_DATABASE
 
| eclipselink.target-database
 
| eclipselink.target-database
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| TABLE_CREATION_SUFFIX
 
| TABLE_CREATION_SUFFIX
 
| eclipselink.ddl-generation.table-creation-suffix
 
| eclipselink.ddl-generation.table-creation-suffix
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| EXCLUDE_ECLIPSELINK_ORM_FILE
 
| EXCLUDE_ECLIPSELINK_ORM_FILE
 
| eclipselink.exclude-eclipselink-orm
 
| eclipselink.exclude-eclipselink-orm
| ignored
 
| processed
 
 
|  
 
|  
|-
 
| WEAVING
 
| eclipselink.weaving
 
| processed
 
| ignored
 
| Composite switches weaving on and off for all members.
 
 
|-
 
|-
 
| WEAVING_*
 
| WEAVING_*
 
| eclipselink.weaving.*
 
| eclipselink.weaving.*
| ignored
+
| If the weaving is on, members use their own weaving properties.
| processed
+
| If the weaving is on then members use their own weaving properties.
+
|-
+
| DESCRIPTOR_CUSTOMIZER_*
+
| eclipselink.descriptor.customizer.*
+
| ignored
+
| processed
+
| TODO? Should we follow the same pattern as SessionCustomizer and process composite's DescriptorCustomizers after members' ones?
+
 
|-
 
|-
 
| NATIVE_QUERY_UPPERCASE_COLUMNS
 
| NATIVE_QUERY_UPPERCASE_COLUMNS
 
| eclipselink.jdbc.uppercase-columns
 
| eclipselink.jdbc.uppercase-columns
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| UPPERCASE_COLUMN_NAMES
 
| UPPERCASE_COLUMN_NAMES
 
| eclipselink.jpa.uppercase-column-names
 
| eclipselink.jpa.uppercase-column-names
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| BATCH_WRITING.*
 
| BATCH_WRITING.*
 
| eclipselink.jdbc.batch-writing.*
 
| eclipselink.jdbc.batch-writing.*
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| SESSION_EVENT_LISTENER_CLASS
 
| SESSION_EVENT_LISTENER_CLASS
 
| eclipselink.session-event-listener
 
| eclipselink.session-event-listener
| processed
+
|This property can also be set on a composite persistence unit. An <tt>EventListener</tt> defined by a composite member will not receive events raised by a <tt>UnitOfWork</tt>; it will only receive events raised by its member. Also see {{bug|348766}}. An <tt>EventListener<>/tt> specified by a composite will receive all events (raised by either a unit of work or by members).
| processed
+
| EventListener defined by a composite member won't receive evens risen by a UnitOfWork - only events risen by it's member. Also see {{bug|348766}}. EventListener specified by composite will receive all event (risen by either unit of work or members).
+
 
|-
 
|-
| EXCEPTION_HANDLER_CLASS
+
| Exception_HANDLER_CLASS
 
| eclipselink.exception-handler
 
| eclipselink.exception-handler
| processed
+
| This property can also be set on a composite persistence unit. The first <tt>ExceptionHandler</tt> specified by a member handles the exception. If it is not specified or fails, then the composite's <tt>ExceptionHandler</tt> handles the original exception.
| processed
+
| First ExceptionHandler specified by member handles exception. If it is not specified or fails then composite's ExceptionHandler handles the original exception.
+
 
|-
 
|-
 
| INCLUDE_DESCRIPTOR_QUERIES
 
| INCLUDE_DESCRIPTOR_QUERIES
 
| eclipselink.session.include.descriptor.queries
 
| eclipselink.session.include.descriptor.queries
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| ID_VALIDATION
 
| ID_VALIDATION
 
| eclipselink.id-validation
 
| eclipselink.id-validation
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| TEMPORAL_MUTABLE
 
| TEMPORAL_MUTABLE
 
| eclipselink.temporal.mutable
 
| eclipselink.temporal.mutable
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| ORM_SCHEMA_VALIDATION
 
| ORM_SCHEMA_VALIDATION
 
| eclipselink.orm.validate.schema
 
| eclipselink.orm.validate.schema
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| DDL_*
 
| DDL_*
 
|  
 
|  
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| PESSIMISTIC_LOCK_TIMEOUT
 
| PESSIMISTIC_LOCK_TIMEOUT
 
| javax.persistence.lock.timeout
 
| javax.persistence.lock.timeout
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| QUERY_TIMEOUT
 
| QUERY_TIMEOUT
 
| javax.persistence.query.timeout
 
| javax.persistence.query.timeout
| ignored
 
| processed
 
 
|  
 
|  
 
|-
 
|-
 
| ORACLE_PROXY_TYPE
 
| ORACLE_PROXY_TYPE
 
| eclipselink.oracle.proxy-type
 
| eclipselink.oracle.proxy-type
| ignored
 
| processed
 
 
|  
 
|  
 
|}
 
|}
  
== Entity Manager Properties ==
+
<br>
* Composite Entity Manager property could be directly passed to createEntityManager method or setProperty method
+
 
* Composite member property could be passed to the same methods through "eclipselink.composite.properties" property.
+
== Entity Manager Properties Set on the Entity Manager ==
 +
 
 +
Entity Manager properties can be set on the <tt>EntityManager</tt> as follows:
 +
* Composite properties can be passed directly to the <tt>createEntityManager()</tt> method or to the <tt>setProperty</tt> method.
 +
* Composite member properties can be passed to the same methods through the <tt>eclipselink.composite.properties</tt> property.
 +
 
 +
The following tables show which properties can be set on composite persistence units and which can be set on composite member persistence units. 
 +
 
 +
===Entity Manager Properties That Can Be Set On Composite Persistence Units===
 +
 
 +
You can set the following entity manager properties on composite persistence units. All other properties will be ignored.
 +
 
 
{|{{BMTableStyle}}
 
{|{{BMTableStyle}}
 
|-{{BMTHStyle}}
 
|-{{BMTHStyle}}
 
| EntityManagerProperties.
 
| EntityManagerProperties.
| property name
+
| Property Name
| composite pu
+
| composite member pu
+
| comment
+
 
|-  
 
|-  
 
| COMPOSITE_PROPERTIES
 
| COMPOSITE_PROPERTIES
 
| eclipselink.composite.properties
 
| eclipselink.composite.properties
| processed
 
| ignored
 
|
 
 
|-
 
|-
 
| FLUSH_CLEAR_CACHE
 
| FLUSH_CLEAR_CACHE
 
| eclipselink.flush-clear.cache
 
| eclipselink.flush-clear.cache
| processed
 
| ignored
 
|
 
 
|-
 
|-
 
| VALIDATE_EXISTENCE
 
| VALIDATE_EXISTENCE
 
| eclipselink.validate-existence
 
| eclipselink.validate-existence
| processed
 
| ignored
 
|
 
 
|-
 
|-
 
| JOIN_EXISTING_TRANSACTION
 
| JOIN_EXISTING_TRANSACTION
 
| eclipselink.transaction.join-existing
 
| eclipselink.transaction.join-existing
| processed
 
| ignored
 
|
 
 
|-
 
|-
 
| PERSISTENCE_CONTEXT_*
 
| PERSISTENCE_CONTEXT_*
 
| eclipselink.persistence-context.*
 
| eclipselink.persistence-context.*
| processed
 
| ignored
 
|
 
 
|-
 
|-
 
| ORDER_UPDATES
 
| ORDER_UPDATES
 
| eclipselink.order-updates
 
| eclipselink.order-updates
| processed
 
| ignored
 
|
 
 
|-
 
|-
 +
|}
 +
 +
===Entity Manager Properties That Can Be Set On Composite Member Persistence Units===
 +
 +
You can set the following entity manager properties on composite member persistence units. All other properties will be ignored.
 +
 +
{|{{BMTableStyle}}
 +
|-{{BMTHStyle}}
 +
| EntityManagerProperties.
 +
| Property Name
 +
|-
 
| EXCLUSIVE_CONNECTION_*
 
| EXCLUSIVE_CONNECTION_*
 
| eclipselink.jdbc.exclusive-connection.*
 
| eclipselink.jdbc.exclusive-connection.*
| ignored
 
| processed
 
|
 
 
|-
 
|-
 
| JDBC_DRIVER
 
| JDBC_DRIVER
 
| javax.persistence.jdbc.driver
 
| javax.persistence.jdbc.driver
| ignored
 
| processed
 
|
 
 
|-
 
|-
 
| JDBC_URL
 
| JDBC_URL
 
| javax.persistence.jdbc.url
 
| javax.persistence.jdbc.url
| ignored
 
| processed
 
|
 
 
|-
 
|-
 
| JDBC_USER
 
| JDBC_USER
 
| javax.persistence.jdbc.user
 
| javax.persistence.jdbc.user
| ignored
 
| processed
 
|
 
 
|-
 
|-
 
| JDBC_PASSWORD
 
| JDBC_PASSWORD
 
| javax.persistence.jdbc.password
 
| javax.persistence.jdbc.password
| ignored
 
| processed
 
|
 
 
|-
 
|-
 
| JTA_DATASOURCE
 
| JTA_DATASOURCE
 
| javax.persistence.jtaDataSource
 
| javax.persistence.jtaDataSource
| ignored
 
| processed
 
|
 
 
|-
 
|-
 
| NON_JTA_DATASOURCE
 
| NON_JTA_DATASOURCE
 
| javax.persistence.nonJtaDataSource
 
| javax.persistence.nonJtaDataSource
| ignored
 
| processed
 
|
 
 
|-
 
|-
 
| CONNECTION_POLICY
 
| CONNECTION_POLICY
 
| eclipselink.jdbc.connection-policy
 
| eclipselink.jdbc.connection-policy
| ignored
 
| processed
 
|
 
 
|-
 
|-
 
| ORACLE_PROXY_TYPE
 
| ORACLE_PROXY_TYPE
 
| eclipselink.oracle.proxy-type
 
| eclipselink.oracle.proxy-type
| ignored
 
| processed
 
|
 
 
|}
 
|}
  
 
== Limitations ==
 
== Limitations ==
* Main limitations' cause is that tables in different data bases cannot be joined in a query. Examples:
+
Composite members persistence units must have unique names in their composite.
** Entities mapped in different composite members could not be joined in a query.
+
 
** If deletion of an entity causes deletion from a table that is mapped by another composite member then delete all query can't be performed.
+
Joins across tables in different data sources are not supported. This limitation affects mapping, query execution, and optimizations. For example:
*** Therefore delete all cannot be used if an entity
+
*Entities mapped in different composite members cannot be joined in a query.
**** has ElementCollection mapped in another composite member,
+
* If deleting an entity causes deletion from a table that is mapped by another composite member, then a "delete all" query cannot be performed. Therefore a delete all cannot be used if an entity:
**** owns bidirectional relationship with a JoinTable with target mapped in another composite member,
+
** has <tt>ElementCollection</tt> mapped in another composite member,
**** has any reference mapping that is privately owned with target mapped in another composite member.
+
** owns bidirectional relationship with a <tt>JoinTable</tt> with the target mapped in another composite member,
* Inheritance hierarchy cannot be shared between different composite members.
+
** has any reference mapping that is privately owned with the target mapped in another composite member.
* If target of a reference mapping is defined in a different composite member and JoinTable is used, then the join table must belong to the target composite member.
+
 
** Such mappings must be unidirectional - the "invert" mapping cannot use "mappedBy".
+
The inheritance hierarchy cannot be shared between different composite members.
*** That's because JoinTable must be defined in the same composite member with target entity: master mapping requires it to be in slave's member, slave mapping - in master's.
+
 
** Workaround is to define independent invert mapping with its own JoinTable.
+
If the target of a reference mapping is defined in a different composite member and <tt>JoinTable</tt> is used, the join table must belong to the target composite member. Such mappings must be unidirectional: the "invert" mapping cannot use <tt>mappedBy</tt>. That is because <tt>JoinTable</tt> must be defined in the same composite member with the target entity. Master mapping requires it to be in slave's member, slave mapping - in master's.You can work around this limitation by defining independent invert mapping with its own <tt>JoinTable</tt>. If you maintain both sides of the relationship, the two join tables will be in sync.
*** If user maintains both sides of the relationship then the two join tables will be in sync.
+
 
* Native query created without result class must specify the target composite member persistence unit.
+
A native query created without a result class must specify the target composite member persistence unit, for example:
 +
 
 
<source lang=java>
 
<source lang=java>
 
em.createNativeQuery("SELECT F_NAME FROM MBR2_EMPLOYEE").setHint(QueryHints.COMPOSITE_UNIT_MEMBER, "composite-advanced-member_2").getResultList();
 
em.createNativeQuery("SELECT F_NAME FROM MBR2_EMPLOYEE").setHint(QueryHints.COMPOSITE_UNIT_MEMBER, "composite-advanced-member_2").getResultList();
Line 610: Line 558:
  
 
== Extensions ==
 
== Extensions ==
* User can specify ElementCollection with primitive type target values with CollectionTable defined in a different composite member persistence unit using
+
You can specify <tt>ElementCollection</tt> with primitive type target values with <tt>CollectionTable</tt> defined in a different composite member persistence unit, using annotations or <tt>eclipselink-orm.xml</tt> as described below:
** Annotations
+
 
 +
* Annotations
 +
 
 
<source lang=java>
 
<source lang=java>
 
@ElementCollection()
 
@ElementCollection()
Line 621: Line 571:
 
}
 
}
 
</source>
 
</source>
** eclipselink-orm.xml
+
 
 +
* eclipselink-orm.xml
 +
 
 
<source lang=xml>
 
<source lang=xml>
 
<element-collection name="responsibilities" composite-member="xml-composite-advanced-member_3">
 
<element-collection name="responsibilities" composite-member="xml-composite-advanced-member_3">
Line 630: Line 582:
 
</element-collection>
 
</element-collection>
 
</source>
 
</source>
** No @CompositeMember annotation (or composite-member attribute) is required if CollectionTable is defined in the same composite member persistence unit as the source.
 
 
== Documentation ==
 
 
== Open Issues ==
 
 
This section lists the open issues that are still pending that must be decided prior to fully implementing this project's requirements.
 
 
{|{{BMTableStyle}}
 
|-{{BMTHStyle}}
 
! Issue #
 
! Description / Notes
 
|-
 
| 1
 
| Should this feature include both split and aggregated persistence units? NO split support.
 
|-
 
| 2
 
| In the case of split persistence units where should the additional '''Data Source'''s be defined? PU properties in persistence.xml, eclipselink-orm.xml, both? Since it is possible for different '''Data Source'''s to be different database vendors or versions it should be possible to configure all JDBC/connection/pool level options on each data source. NO split support.
 
|-
 
| 3
 
| Should this feature include support to customize additional '''Data Source'''s with persistence unit properties. NO split support.
 
|-
 
| 4
 
| In the case of aggregate persistence units, should persistence units be usable on their own. YES, if independent.
 
|}
 
 
== Decisions ==
 
  
This section lists decisions made. These are intended to document the resolution of open issues or constraints added to the project that are important.
+
No <tt>@CompositeMember</tt> annotation (or <tt>composite-member</tt> attribute) is required if <tt>CollectionTable</tt> is defined in the same composite member persistence unit as the source.

Latest revision as of 12:13, 19 July 2011

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source


Composite Persistence Units

EL NewIn.png New in version 2.3.



Multiple persistence units with unique sets of entity types can be exposed as a single persistence context by using a composite persistence unit. A persistent unit that is part of a composite persistent unit is called a composite member persistent unit.

With composite persistence units:

  • Mapping relationships can be established among any of the entities in the composite.
  • The persistence context can access entities stored in different data sources.
  • Queries and transactions can be performed transparently across the complete set of entities.

For example, you could persist data from a single context into different data sources, as shown below:

em.persist(new A(..));
em.persist(new Z(..));
// Insert A into database1; insert Z into database2:
// the two databases can be from different vendors.
em.flush();


Configuring Composite Persistence and Composite Member Persistence Units

Both composite persistence units and their member persistence units are regular persistence units, each of them must have persistence.xml.

Configuring Composite Persistent Units in persistence.xml

  • Use the <property name="eclipselink.composite-unit" value="true"/> property identify persistence unit as a composite. (Note: If this property is passed to the createEntityManagerFactory method or if it is set in system properties, it is ignored.)
  • Use the <jar-file> element to specify the jar files containing the composite member persistent units. The composite persistence unit will contain all the persistence units found in the jar files specified.
  • The transaction type may be specified in the composite persistence unit (otherwise default transaction type is used). Transaction types of composite members are ignored.
  • Some persistence unit properties apply to the composite persistence unit (like "eclipselink.target-server") and may be specified in composite pu persistence.xml (these properties ignored if specified in composite members). For complete list of these properties see the table ...
  • Don't specify any classes in composite persistence unit - all the classes should be specified in composite members.

Example

The composite persistence unit compositePU specifies the transaction type and the server platform. It will contain all persistence units defined in member1.jar and member2.jar files.

<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>

Configuring Composite Member Persistent Units in persistence.xml

  • Data sources may be specified in composite member persistence units. Those specified in the composite are ignored.
  • Some persistence unit properties apply to the composite persistence unit member only (like "eclipselink.target-database") and may be specified in composite members pu persistence.xml (these properties ignored if specified in the composite pu). For complete list of these properties see the table ...
  • Normally composite member pu could also be used as stand alone pu (independent from the composite). However if a member pu has dependencies of another member(s) (for instance a class defined in memberPu2 has a reference to a class defined in memberPu1) then such pu can no longer be used as a stand alone - only as a member of the composite.
    • To indicate that a member pu must be a member of a composite pu <property name="eclipselink.composite-unit.member" value="true"/> should be specified in its persistence.xml. (Note: If this property is passed to the createEntityManagerFactory method or if it is set in system properties, it is ignored.)
      • Attempt to create an EntityManagerFactory for such pu will still succeed (that's required by application servers), however attempt to create an EntityManager from the factory would fail

Example

Composite member persistence unit memberPu1 is defined in the member1.jar file. It can be used independently as well as inside composite persistence unit. Note that transaction type and server platform will be ignored if memberPu1 used as a composite member pu, but they will be used if memberPu1 is used stand alone (as a regular 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>

Composite member persistence unit memberPu2 is defined in the member2.jar file. It has dependency on a class defined in member1.jar and cannot be used independently.

<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="memberPu2">
        <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.composite-unit.member" value="true"/>
            <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
        </properties>
    </persistence-unit>
</persistence>

Passing Persistence Unit Properties to Composite Member Persistent Units

As with regular persistence units, you can override properties specified in persistence.xml with new ones passed to the createEntityManagerFactory method. Therefore, you can change some properties at runtime, for example connection parameters. To do this with composite member persistence units, use the eclipselink.composite-unit.properties property. The value is a Map that is keyed on composite member persistence unit names; the value is a Map that contains all properties to be passed to this member persistence unit. For example,

Map props1 = new HashMap();
 
   props1.put(PersistenceUnitProperties.JDBC_USER, "user1");
   props1.put(PersistenceUnitProperties.JDBC_PASSWORD, "password1");
   props1.put(PersistenceUnitProperties.JDBC_DRIVER, "oracle.jdbc.OracleDriver");
   props1.put(PersistenceUnitProperties.JDBC_URL, "jdbc:oracle:thin:@oracle_db_url:1521:db");
 
Map props2 = new HashMap();
 
   props2.put(PersistenceUnitProperties.JDBC_USER, "user2");
   props2.put(PersistenceUnitProperties.JDBC_PASSWORD, "password2");
   props2.put(PersistenceUnitProperties.JDBC_DRIVER, "oracle.mysql.OracleDriver");
   props2.put(PersistenceUnitProperties.JDBC_URL, " jdbc:mysql:@oracle_sql_url:3306/user2");
 
Map memberProps = new HashMap();
   memberProps.put("memberPu1", props1);
   memberProps.put("memberPu2", props2);
 
Map props = new HashMap();
   props.put("eclipselink.composite-unit.properties", memberProps);
 
EntityManagerFactory emf = Persistence.createEntityManagerFactory("composite", props);

Only persistence unit properties that are processed by composite members (as indicated in the Persistence Unit Properties table, below) should be passed to a member. Other properties are ignored. The same rule applies for the createEntityManager method; only those entity manager properties that are processed on members (as indeicated in the Entity Manager Properties table) should be passed. Other properties are ignored.

Deploying and Accessing a Composite Persistence Unit

All three jars – the composite and the two members– should be deployed on the same class loader. If they are deployed to an application server, the jars should be packed in an ear file. If they run standalone, the jars should be added to the class path.

A composite persistence unit can be accessed as with any other persistence unit.

  • It can be accessed using injection, for example:
@PersistenceUnit(unitName="compositePu")
EntityManagerFactory entityManagerFactory;
@PersistenceUnit(unitName="compositePu")
EntityManager entityManager;
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("compositePu", properties);

Persistence Unit Properties Set on the Entity Manager Factory

Persistence unit properties can be set on the EntityManagerFactory as follows:

  • Composite persistence unit properties can be specified either in the composite persistence unit's persistence.xml file or passed to the its createEntityManagerFactory() method.
  • Composite member persistence unit properties can either be specified in the composite member persistence unit's persistence.xml file or passed to the composite persistence unit's createEntityManagerFactory() method through the eclipselink.composite.properties property.

The following tables show where to set properties: on the composite persistence unit or on the composite member persistence unit. You should only pass properties to a composite or to a member that can be processed by the composite or the member. That is,

  • Only pass to a composite persistence unit those properties that are processed on composite persistence units. All other properties are ignored.
  • Only pass to a composite persistence unit member (through eclipselink.composite.properties) the properties that are processed on composite member persistence units. All other properties are ignored.

For complete PersistenceUnitProperties reference documentation, see [|PersistenceUnitProperties].

Persistence Unit Properties That Can Be Set on Composite Persistence Units

You can set the following persistence unit properties on composite persistence units. All other properties will be ignored.

PersistenceUnitProperties. Property Name comment
COMPOSITE_UNIT eclipselink.composite-unit Must be specified in persistence.xml of a composite persistence unit with the value "true". Advanced usage: This property can also be set on a composite member persistence unit that is itself a composite persistence unit. In that case, the composite is substituted by its own members.
COMPOSITE_PROPERTIES eclipselink.composite.properties
TRANSACTION_TYPE javax.persistence.transactionType
TARGET_SERVER eclipselink.target-server
LOGGING_* eclipselink.logging.*
PROFILER eclipselink.profiler
COORDINATION_* eclipselink.cache.coordination.*
SESSION_NAME eclipselink.session-name This property can also be set on a composite member persistence unit . The member session name is used to read the session from sessions.xml, but the deployed session always has the same name as the member persistence unit (as it is defined in memberPuInfo.getPersistenceUnitName())
DEPLOY_ON_STARTUP eclipselink.deploy-on-startup
VALIDATION_ONLY_PROPERTY eclipselink.validation-only In 2.3: This property is also processed if set on a composite member persistence unit, but that is a known bug (bug 348815). This property should be processed by the composite only and ignored by member persistence units. To work around this bug, specify the property in the composite and in all its members. THIS IS FIXED IN 2.3.1: it's ignored by members.
VALIDATION_* eclipselink.validation.*
CLASSLOADER eclipselink.classloader
THROW_EXCEPTIONS eclipselink.orm.throw.exceptions
FLUSH_CLEAR_CACHE eclipselink.flush-clear.cache
VALIDATE_EXISTENCE eclipselink.validate-existence
JOIN_EXISTING_TRANSACTION eclipselink.transaction.join-existing
PERSISTENCE_CONTEXT_* eclipselink.persistence-context.*
ALLOW_ZERO_ID eclipselink.allow-zero-id
SESSION_CUSTOMIZER eclipselink.session.customizer This property can also be set on a composite member persistence unit. Customizers of member persistence units are processed before the composite persistence unit's customizer.
SESSION_EVENT_LISTENER_CLASS eclipselink.session-event-listener This property can also be set on a composite member persistence unit. An

EventListener defined by a composite member will not receive events raised by a UnitOfWork; it will only receive events raised by its member. Also see bug 348766 - FIXED in 2.3.1. An EventListener<>/tt> specified by a composite will receive all events (raised by either a unit of work or by members).

Exception_HANDLER_CLASS eclipselink.exception-handler This property can also be set on a composite member persistence unit. The first <tt>ExceptionHandler specified by a member handles the exception. If it is not specified or fails, then the composite's ExceptionHandler handles the original exception.
WEAVING eclipselink.weaving A composite persistent unit switches weaving on and off for all its member persistent units.

Persistence Unit Properties That Can Be Set on Composite Member Persistence Units

You can set the following persistence unit properties on composite member persistence units. All other properties will be ignored.

PersistenceUnitProperties. Property Name comment
COMPOSITE_UNIT eclipselink.composite-unit Advanced usage: This property can also be set on a composite member persistence unit that is itself a composite persistence unit. In that case, the composite is substituted by its own members.
COMPOSITE_UNIT_MEMBER eclipselink.composite-unit.member The property should be specified with a value "true" in persistence.xml to indicate that the persistence unit cannot be used stand alone (only as a member of a composite).
SESSION_NAME eclipselink.session-name This property can also be set on a composite persistence unit. The member session name is used to read the session from sessions.xml, but the deployed session always has the same name as the member persistence unit (as it is defined in memberPuInfo.getPersistenceUnitName())
VALIDATION_ONLY_PROPERTY eclipselink.validation-only This property is processed if set on a composite member persistence unit, but that is a known bug (bug 348815). This property should be processed by the composite only and ignored by member persistence units. To work around this bug, specify the property in the composite and in all its members. FIXED in 2.3.1 - ignored on member.
VALIDATION_* eclipselink.validation.*
CLASSLOADER eclipselink.classloader
THROW_EXCEPTIONS eclipselink.orm.throw.exceptions
FLUSH_CLEAR_CACHE eclipselink.flush-clear.cache
VALIDATE_EXISTENCE eclipselink.validate-existence
JOIN_EXISTING_TRANSACTION eclipselink.transaction.join-existing
PERSISTENCE_CONTEXT_* eclipselink.persistence-context.*
ALLOW_ZERO_ID eclipselink.allow-zero-id
SESSION_CUSTOMIZER eclipselink.session.customizer This property can also be set on a composite persistence unit. Customizers of member persistence units are processed before the composite persistence unit's customizer.
SESSIONS_XML eclipselink.sessions-xml If this property is set on a composite persistence unit, an exception will be thrown. Composite persitence units cannot be read from sessions.xml. Note that a non-composite persistence unit should be able to use a SessionBroker defined in sessions.xml.
JTA_DATASOURCE javax.persistence.jtaDataSource
NON_JTA_DATASOURCE javax.persistence.nonJtaDataSource
NATIVE_SQL eclipselink.jdbc.native-sql
SQL_CAST eclipselink.jdbc.sql-cast
JDBC_* javax.persistence.jdbc.* eclipselink.jdbc.*
CONNECTION_POOL_*
PARTITIONING.* eclipselink.partitioning.*
EXCLUSIVE_CONNECTION_* eclipselink.jdbc.exclusive-connection.*
CACHE.* eclipselink.cache.*
TARGET_DATABASE eclipselink.target-database
TABLE_CREATION_SUFFIX eclipselink.ddl-generation.table-creation-suffix
EXCLUDE_ECLIPSELINK_ORM_FILE eclipselink.exclude-eclipselink-orm
WEAVING_* eclipselink.weaving.* If the weaving is on, members use their own weaving properties.
NATIVE_QUERY_UPPERCASE_COLUMNS eclipselink.jdbc.uppercase-columns
UPPERCASE_COLUMN_NAMES eclipselink.jpa.uppercase-column-names
BATCH_WRITING.* eclipselink.jdbc.batch-writing.*
SESSION_EVENT_LISTENER_CLASS eclipselink.session-event-listener This property can also be set on a composite persistence unit. An EventListener defined by a composite member will not receive events raised by a UnitOfWork; it will only receive events raised by its member. Also see bug 348766. An EventListener<>/tt> specified by a composite will receive all events (raised by either a unit of work or by members).
Exception_HANDLER_CLASS eclipselink.exception-handler This property can also be set on a composite persistence unit. The first <tt>ExceptionHandler specified by a member handles the exception. If it is not specified or fails, then the composite's ExceptionHandler handles the original exception.
INCLUDE_DESCRIPTOR_QUERIES eclipselink.session.include.descriptor.queries
ID_VALIDATION eclipselink.id-validation
TEMPORAL_MUTABLE eclipselink.temporal.mutable
ORM_SCHEMA_VALIDATION eclipselink.orm.validate.schema
DDL_*
PESSIMISTIC_LOCK_TIMEOUT javax.persistence.lock.timeout
QUERY_TIMEOUT javax.persistence.query.timeout
ORACLE_PROXY_TYPE eclipselink.oracle.proxy-type


Entity Manager Properties Set on the Entity Manager

Entity Manager properties can be set on the EntityManager as follows:

  • Composite properties can be passed directly to the createEntityManager() method or to the setProperty method.
  • Composite member properties can be passed to the same methods through the eclipselink.composite.properties property.

The following tables show which properties can be set on composite persistence units and which can be set on composite member persistence units.

Entity Manager Properties That Can Be Set On Composite Persistence Units

You can set the following entity manager properties on composite persistence units. All other properties will be ignored.

EntityManagerProperties. Property Name
COMPOSITE_PROPERTIES eclipselink.composite.properties
FLUSH_CLEAR_CACHE eclipselink.flush-clear.cache
VALIDATE_EXISTENCE eclipselink.validate-existence
JOIN_EXISTING_TRANSACTION eclipselink.transaction.join-existing
PERSISTENCE_CONTEXT_* eclipselink.persistence-context.*
ORDER_UPDATES eclipselink.order-updates

Entity Manager Properties That Can Be Set On Composite Member Persistence Units

You can set the following entity manager properties on composite member persistence units. All other properties will be ignored.

EntityManagerProperties. Property Name
EXCLUSIVE_CONNECTION_* eclipselink.jdbc.exclusive-connection.*
JDBC_DRIVER javax.persistence.jdbc.driver
JDBC_URL javax.persistence.jdbc.url
JDBC_USER javax.persistence.jdbc.user
JDBC_PASSWORD javax.persistence.jdbc.password
JTA_DATASOURCE javax.persistence.jtaDataSource
NON_JTA_DATASOURCE javax.persistence.nonJtaDataSource
CONNECTION_POLICY eclipselink.jdbc.connection-policy
ORACLE_PROXY_TYPE eclipselink.oracle.proxy-type

Limitations

Composite members persistence units must have unique names in their composite.

Joins across tables in different data sources are not supported. This limitation affects mapping, query execution, and optimizations. For example:

  • Entities mapped in different composite members cannot be joined in a query.
  • If deleting an entity causes deletion from a table that is mapped by another composite member, then a "delete all" query cannot be performed. Therefore a delete all cannot be used if an entity:
    • has ElementCollection mapped in another composite member,
    • owns bidirectional relationship with a JoinTable with the target mapped in another composite member,
    • has any reference mapping that is privately owned with the target mapped in another composite member.

The inheritance hierarchy cannot be shared between different composite members.

If the target of a reference mapping is defined in a different composite member and JoinTable is used, the join table must belong to the target composite member. Such mappings must be unidirectional: the "invert" mapping cannot use mappedBy. That is because JoinTable must be defined in the same composite member with the target entity. Master mapping requires it to be in slave's member, slave mapping - in master's.You can work around this limitation by defining independent invert mapping with its own JoinTable. If you maintain both sides of the relationship, the two join tables will be in sync.

A native query created without a result class must specify the target composite member persistence unit, for example:

em.createNativeQuery("SELECT F_NAME FROM MBR2_EMPLOYEE").setHint(QueryHints.COMPOSITE_UNIT_MEMBER, "composite-advanced-member_2").getResultList();

Extensions

You can specify ElementCollection with primitive type target values with CollectionTable defined in a different composite member persistence unit, using annotations or eclipselink-orm.xml as described below:

  • Annotations
@ElementCollection()
@CollectionTable(name = "MBR1_RESPONS", joinColumns=@JoinColumn(name="EMP_ID"))
@CompositeMember("composite-advanced-member_1")
@Column(name = "DESCRIPTION")
public Collection<String> getResponsibilities() {
    return responsibilities;
}
  • eclipselink-orm.xml
<element-collection name="responsibilities" composite-member="xml-composite-advanced-member_3">
    <column name="DESCRIPTION"/>
    <collection-table name="XML_MBR3_RESPONS">
        <join-column name="EMP_ID"/>
    </collection-table>
</element-collection>

No @CompositeMember annotation (or composite-member attribute) is required if CollectionTable is defined in the same composite member persistence unit as the source.

Back to the top