Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

The top level element of the persistence.xml is <persistence>. The below sections explain the sub-elements of the <persistence> element. Typical persistence.xml looks like below.

Code Block
XML
XML
borderStylesolid
titlepersistence.xmlXML
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
 ...
 ...
</persistence>

...

Note

1. The below annotation injects EntityManager object that corresponds to Tutorial persistence unit
to the variable em .

Code Block
JAVA
JAVA
borderStylesolidJAVA
  @PersistenceContext(unitName="Tutorial")
  private EntityManager em; 

2. The below annotation injects EntityManagerFactory object that corresponds to Account
persistence unit to the variable emf. From the emf, the EntityManager object can be created.

Code Block
JAVA
JAVA
borderStylesolidJAVA
@PersistenceUnit(unitName="Account")
private EntityManagerFactory emf;

...

Note

1. The below declaration is for a persistence unit named Account and the transaction type is JTA.

Code Block
XML
XML
borderStylesolidXML
<persistence-unit name="Account" transaction-type="JTA">
...
...
</persistence-unit>

2. The below declaration is for a persistence unit named Account and the transaction type is
RESOURCE_LOCAL.

Code Block
XML
XML
borderStylesolidXML
 
<persistence-unit name="Account" transaction-type="RESOURCE_LOCAL">
...
...
</persistence-unit>

Please note that there are two types of entity managers and corresponding persistence contexts. These are container managed entity manager and application managed entity manager.

The container managed entity manager is the one whose persistence context is managed by container. The persistence context is propagated along the active transaction. The persistence scope of the container managed entity manager is transaction by default. The transaction type of the entity manager is always JTA. The EntityManager object is injected by the container to the application.

The application managed entity manager is the one whose persistence context is managed by the application. The persistence context is not propagated along the transaction. The persistence context will be active even after the current transaction completes. The transaction type of the entity manager is RESOURCE_LOCAL by default. The EntityManager object should be created by using EntityManagerFactory object in the application.

...

Note

1. The below XML content illustrate the use of the element.

Code Block
XML
XML
borderStylesolidXML
 
<persistence-unit name="Account" transaction-type="JTA">
 <description>Entities related to bank accounts</description>
 ...                 
 ...
</persistence-unit>

...

Note

1. The below XML content illustrate the use of the element.

Code Block
XML
XML
borderStylesolidXML
<persistence-unit name="Account" transaction-type="JTA">
<description>Entities related to bank accounts</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
...
...
</persistence-unit>

For OpenJPA, the value of the provider is org.apache.openjpa.persistence.PersistenceProviderImpl.

...

Note

In the geronimo server, users can deploy datasources (database connection pools) on databases from various vendors like DB2, Oracle, MySQL, MS-SQLServer etc,. When creating a datasource, users have to specify the datasource name in the deployment plan. The name given to the datasource is to be provided in the jta-data-source and non-jta-data-source elements. The below XML fragment illustrates the usage.

Code Block
XML
XML
borderStylesolidXML
<persistence-unit name="Account" transaction-type="JTA">
  <description>ContainerManagedJPA</description>
  <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
  <jta-data-source>AccountDB2DataSource</jta-data-source>
  ...
  ...
</persistence-unit>

The non-jta-datasource must be a datasource that has no transaction support. It can be deployed on the geronimo server by using the <no-transaction/> element instead of <local-transaction/> or <xa-transaction> in the connector plan as illustrated below.

Code Block
XML
XML
borderStylesolidXML
<connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2">
...
...
 <resourceadapter>
 ...
 ...
  <connectionmanager>
   <local-transaction/>
    <no-transaction/>
     <max-size>10</max-size>
     <min-size>0</min-size>
     <match-one/>
    </single-pool>
  </connectionmanager>
 ...
 ...
 </resourceadapter>
...
...
</connector>

...

An orm.xml file or other mapping file is loaded as a resource by the persistence provider. If a mapping file is specified, the classes and mapping information specified in the mapping file will be used. If multiple mapping files are specified (possibly including one or more orm.xml files), the resulting mappings are obtained by combining the mappings from all of the files. The result is undefined if multiple mapping files (including any orm.xml file) referenced within a single persistence unit contain overlapping mapping information for any given class. The below XML fragment illustrates the usage.

Note
Code Block
XML
XML
borderStylesolidXML
<persistence-unit name="Tutorial" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<non-jta-data-source>ProductDS-nonJTA</non-jta-data-source>
<mapping-file>object_mappings.xml</mapping-file>
     ...
     ...
</persistence-unit>

The object_mappings.xml file must be in the classpath of the application.

...

  • A list of named managed persistence classes may also be specified instead of, or in addition to, the JAR files and mapping files (using class xml element explained below). Any mapping metadata annotations found on these classes will be processed, or they will be mapped using the mapping annotation defaults. The following XML fragment illustrates the usage.
    Note
    Code Block
    XML
    XML
    borderStylesolidXML
    <persistence-unit name="Tutorial" transaction-type="RESOURCE_LOCAL">
     <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
     <jta-data-source>AccountDS</jta-data-source>
     <mapping-file>account_mappings.xml</mapping-file>
     <jar-file>account-entities.jar</jar-file>
      ...
      ...
    </persistence-unit>
    

    The account-entities.jar is placed at the root of the persistence unit.

...

The following XML fragment illustrate the use of class element.

Note
Code Block
XML
XML
borderStylesolidXML
<persistence-unit name="AccountUnit" transaction-type="JTA">
  <description>ContainerManagedJPA</description>
  <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
  <jta-data-source>AccountDS</jta-data-source>
  <class>sample.jpa.Account</class>
  <class>sample.jpa.Person</class>
  ...
  ...
</persistence-unit> 

The sample.jpa.Account and sample.jpa.Person are explicitly mentioned as managed persistence classes in the persistence.xml.

...

When set to true, only listed classes and jars will be scanned for persistent classes. Otherwise the enclosing jar or directory will also be scanned. This is not applicable to Java SE persistence units. The following XML fragment illustrate the use of exclude-unlisted-classes element.

Note
Code Block
XML
XML
borderStylesolidXML
<persistence-unit name="AccountUnit" transaction-type="JTA">
  <description>ContainerManagedJPA</description>
  <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
  <jta-data-source>AccountDS</jta-data-source>
  <jar-file>account-entities.jar</jar-file>
  <class>sample.jpa.Account</class>
  <class>sample.jpa.Person</class>
  <exclude-unlisted-classes>true</exclude-unlisted-classes>
  ...
  ...
 </persistence-unit> 

Only account-entities.jar, sample.jpa.Account and sample.jpa.Person are scanned for managed persistence classes.

...

The following XML fragment illustrate the use of properties element. The specified properties and the values enable JPA to connect to VehicleDB database created in the embedded derby of geronimo server.

Note
Code Block
XML
XML
borderStylesolidXML
<persistence-unit name="Inheritence">
 <description>Single Table Inheritence example</description>
 <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
 <class>com.jpa.RoadVehicle</class>
 <class>com.jpa.Car</class>
 <class>com.jpa.Coupe</class>
 <class>com.jpa.Motorcycle</class>
 <class>com.jpa.Roadster</class>
 <properties>
   <property name="openjpa.ConnectionURL" value="jdbc:derby:VehicleDB" />
   <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver" />
   <property name="ConnectionUserName" value="app" />
   <property name="openjpa.jdbc.SynchronizeMappings" value="false" />
 </properties>
</persistence-unit>

The above properties are specific to OpenJPA provider. The OpenJPA provider reads these properties and creates appropriate EntityManagerFactory. The properties supported by OpenJPA is at this link

...

  • The below example illustrate the overriding feature in geronimo.
Note
Code Block
XML
XML
borderStylesolidXML
<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence  
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

 <persistence-unit name="AccountUnit" transaction-type="JTA">
  <description>ContainerManagedJPA</description>
  <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
  <jta-data-source>AccountDS</jta-data-source>
  <class>sample.jpa.Account</class>
  <class>sample.jpa.Person</class>
 </persistence-unit>

</persistence>

The AccountUnit can be overridden in openejb-jar.xml as follows.

Code Block
XML
XML
borderStylesolidXML
<openejb-jar xmlns="http://openejb.apache.org/xml/ns/openejb-jar-2.2" 
 xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2" 
 xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" 
 xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
 
 <sys:environment>

  <sys:moduleId>
   <sys:groupId>ContainerManagedJPA</sys:groupId>
   <sys:artifactId>EJB</sys:artifactId>
   <sys:version>1.0</sys:version>
   <sys:type>car</sys:type>
  </sys:moduleId>

 <dependencies>
  <dependency>
   <groupId>console.dbpool</groupId>
   <artifactId>AccountDS1</artifactId>
  </dependency>
 </dependencies>

 </sys:environment>

 <persistence xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="AccountUnit" transaction-type="JTA">
   <description>ContainerManagedJPA</description>
   <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
   <jta-data-source>AccountDS1</jta-data-source>
   <class>sample.jpa.Account</class>
   <class>sample.jpa.Person</class>
  </persistence-unit>
 </persistence>
 
 <enterprise-beans/>

</openejb-jar>

The AccountUnit is overridden in openejb-jar.xml to use the JTA datasource AccountDS1. In the persistence.xml, it was declared to use the JTA datasource AccountDS.

Similarly, the persistence-units can be overridden in geronimo-web.xml right after the <sys:environment> declaration.