Versions Compared

Key

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

...

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

...

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

...

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

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
Code Block
XML
XML
borderStylesolid

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

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