...
Schema top level elements
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 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>
|
<persistence-unit>
The <persistence> element can consist of more than one <persistence-unit> element each describing a persistence unit. The persistence-unit element consists of the following attributes.
Attribute : name
This attribute is required. The name attribute defines the name for the persistence unit. It uniquely identifies a persistence context. This name is used to identify a persistence unit referred to by the PersistenceContext and PersistenceUnit annotations. It is also referred while creating an entity manager factory object. The following note illustrates the usage in an application.
Note | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. The below annotation injects EntityManager object that corresponds to Tutorial persistence unit
2. The below annotation injects EntityManagerFactory object that corresponds to Account
|
Attribute : transaction-type
This attribute can have the following values.
- JTA
- RESOURCE_LOCAL
The transaction-type attribute is used to specify whether the entity managers provided by the entity manager factory for the persistence unit must be JTA entity managers or resource-local entity managers. The value of this element is JTA or RESOURCE_LOCAL. A transaction-type of JTA assumes that a JTA data source will be provided. It is provided either as specified by the jta-data-source element or provided by the container. In general, in Java EE environments, a transaction-type of RESOURCE_LOCAL assumes that a non-JTA datasource will be provided. In a Java EE environment, if this element is not specified, the default is JTA. In a Java SE environment, if this element is not specified, a default of RESOURCE_LOCAL may be assumed.
Note | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1. The below declaration is for a persistence unit named Account and the transaction type is JTA.
2. The below declaration is for a persistence unit named Account and the transaction type is
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. |
<description>
The description element provides optional descriptive information about the persistence unit.
Note | |||||||||
---|---|---|---|---|---|---|---|---|---|
1. The below XML content illustrate the use of the element.
|
<provider>
The provider element specifies the name of the persistence provider's javax.persistence.spi.PersistenceProvider
class. The provider element must be specified if the application is dependent upon a particular persistence provider being used. The provider class supplies EntityManagers for this persistence unit.
Note | |||||||||
---|---|---|---|---|---|---|---|---|---|
1. The below XML content illustrate the use of the element.
For OpenJPA, the value of the provider is org.apache.openjpa.persistence.PersistenceProviderImpl. |
<jta-data-source>, <non-jta-data-source>
In Java EE environments, the jta-data-source and non-jta-data-source elements are used to specify the global JNDI name of the JTA and non-JTA data source respectively to be used by the persistence provider. If neither is specified, the deployer must specify a JTA data source at deployment or a JTA data source must be provided by the container, and a JTA EntityManagerFactory will be created to correspond to it. These elements name the data source in the local environment. The format/syntax of these names and the ability to specify the names are product specific. In Java SE environments, these elements may be used or the data source information may be specified by other means; depending upon the requirements of the provider.
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.
|
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
<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>
|
<mapping-file>
An object/relational mapping XML file contains mapping information for the classes listed in it. The mapping files can be provided as follows.
- Object/relational mapping XML file named orm.xml may be specified in the META-INF directory in the root of the
persistence unit.
- Object/relational mapping XML file named orm.xml may be specified in the META-INF directory of any jar file
referenced by the persistence.xml.
- In addition, other mapping files may be referenced by the mapping-file elements of the persistence-unit
element, and may be present anywhere on the classpath.
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 | |||||||||
---|---|---|---|---|---|---|---|---|---|
The object_mappings.xml file must be in the classpath of the application. |
<jar-file>
One or more JAR files may be specified using the jar-file elements instead of, or in addition to the mapping files specified in the mapping-file elements. If specified, JPA processes these jar files as follows.
- These JAR files will be searched for managed persistence classes, and any mapping metadata annotations found on them will be processed.
- They will be mapped using the mapping annotation defaults defined by the JPA specification. JAR files are specified relative to the root of the persistence unit.
- 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 borderStyle solid <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.
<Class>
The following classes must be implicitly or explicitly denoted as managed persistence classes to be
included within a persistence unit.
- Entity classes
- Embeddable classes
- Mapped superclasses.
The set of managed persistence classes that are managed by a persistence unit is defined by using one or
more of the following:
- One or more object/relational mapping XML files (explained in mapping-file section above).
- One or more jar files that will be searched for classes explained in jar-file section above).
- An explicit list of the classes (using class element).
- The annotated managed persistence classes contained in the root of the persistence unit
(unless the exclude-unlisted-classes element is specified)
In the Java SE environment, the following rules and recommendations are required to be followed.
- The class element is used to list a managed persistence class. A list of all named managed persistence classes must be specified in Java SE environments to insure portability.
- Portable Java SE applications should not rely on the other mechanisms described here to specify the managed persistence classes of a persistence unit.
- Persistence providers may also require that the set of entity classes that are to be managed must be fully enumerated in persistence.xml.
The following procedure explains how managed persistence classes are searched.
- All classes contained in the root of the persistence unit are also searched for annotated managed persistence classes and any mapping metadata annotations found on them will be processed, or they will be mapped using the mapping annotation defaults.
- If it is not intended that the annotated persistence classes contained in the root of the persistence unit be included in the persistence unit, the exclude-unlisted-classes element should be used.
- The exclude-unlisted-classes element is not intended for use in Java SE environments.
- The resulting set of entities managed by the persistence unit is the union of these sources, with the mapping metadata annotations (or annotation defaults) for any given class being overridden by the XML mapping information file if there are both annotations as well as XML mappings for that class.
- The minimum portable level of overriding is at the level of the persistent field or property.
- The classes and/or jars that are named as part of a persistence unit must be on the classpath. Referencing them from the persistence.xml file does not cause them to be placed on the classpath.
- All classes must be on the classpath to ensure that entity managers from different persistence units that map the same class will be accessing the same identical class.
The following XML fragment illustrate the use of class element.
Note | |||||||||
---|---|---|---|---|---|---|---|---|---|
The sample.jpa.Account and sample.jpa.Person are explicitly mentioned as managed persistence classes in the persistence.xml. |
<exclude-unlisted-classes>
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 | |||||||||
---|---|---|---|---|---|---|---|---|---|
Only account-entities.jar, sample.jpa.Account and sample.jpa.Person are scanned for managed persistence classes. |
<properties>
- The properties element is used to specify vendor-specific properties that apply to the persistence unit and its entity manager factory configuration.
- If a persistence provider does not recognize properties (other than those defined by this specification), the provider will ignore those properties.
- Vendors will use vendor namespaces for properties.
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 | |||||||||
---|---|---|---|---|---|---|---|---|---|
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 |
JPA configuration and geronimo plans
The persistence.xml file can declare more than one persistence-units. A persistence-unit declaration in the persistence.xml can be overridden in geronimo plans (geronimo-web.xml or openejb-jar.xml) as follows.
- If a persistence-unit is declared in both in persistence.xml as well as in a geronimo plan
(geronimo-web.xml or openejb-jar.xml), the declaration in the geronimo plan will override the declaration
in the persistence.xml
- The below example illustrate the overriding feature in geronimo.
Note | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
The AccountUnit can be overridden in openejb-jar.xml as follows.
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. |