|Home > Documentation > Creating deployment plans > Understanding deployment plans > persistence.xml|
The persistence.xml file describes persistence units. It is the deployment descriptor file for persistence using Java Persistence API (JPA). It is used to declare the following.
The persistence.xml file is placed in the META-INF directory of the root of the persistence unit. The object/relational mapping information is provided by the following ways.
In Java EE, the root of a persistence unit can be one of the following
The location of the managed persistence classes can be as follows.
The persistence.xml file is placed in the META-INF directory of the root of the persistence unit.
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.
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.
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.
This attribute can have the following values.
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.
The description element provides optional descriptive information about the persistence unit.
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.
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.
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.
An object/relational mapping XML file contains mapping information for the classes listed in it. The mapping files can be provided as follows.
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.
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.
The following classes must be implicitly or explicitly denoted as managed persistence classes to be
included within a persistence unit.
The set of managed persistence classes that are managed by a persistence unit is defined by using one or
more of the following:
In the Java SE environment, the following rules and recommendations are required to be followed.
The following procedure explains how managed persistence classes are searched.
The following XML fragment illustrate the use of class element.
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.
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.
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.