You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

Overview

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.

  • Managed persistence classes.
    The managed classes are, for example, those which are annotated using @Entity, @Embeddable or
    @MappedSuperclass.
  • Specify object/relation mapping.
    JPA provides several mechanisms to map the java classes to tables in a relational database.
  • Configuration information for entity managers and entity manager factory classes.

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.

  • Annotations on the managed persistence classes
  • One or more XML files contained in the root of the persistence unit
  • One or more XML files outside the root of the persistence unit on the classpath and referenced from the
    persistence.xml, or a combination of these.

In Java EE, the root of a persistence unit can be one of the following

  • EJB-JAR file
  • WEB-INF/classes directory of a WAR file
  • A jar file in the WEB-INF/lib directory of a WAR file
  • A jar file in the root of the EAR
  • A jar file in the EAR library directory
  • An application client jar file

The location of the managed persistence classes can be as follows.

  • Within the root of the persistence unit.
  • Can be specified by reference in the persistence.xml by naming the classes, archives, or mapping XML files
    that are accessible on the application classpath.
  • Some combinations of the above methods.

Packaging

The persistence.xml file is placed in the META-INF directory of the root of the persistence unit.

Schema

The schema of the persistence.xml is at this link. Apache geronimo uses OpenJPA as the JPA provider.

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.

persistence.xml
<?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>
  • No labels