background image

Persistence Units

<< Named Parameters in Queries | JAR Files >>
<< Named Parameters in Queries | JAR Files >>

Persistence Units

.setParameter(1, name)
.getResultList();
}
Input parameters are numbered starting from 1. Input parameters are case-sensitive, and may
be used by both dynamic and static queries.
Persistence Units
A persistence unit defines a set of all entity classes that are managed by EntityManager
instances in an application. This set of entity classes represents the data contained within a
single data store.
Persistence units are defined by the persistence.xml configuration file. The JAR file or
directory whose META-INF directory contains persistence.xml is called the root of the
persistence unit. The scope of the persistence unit is determined by the persistence unit's root.
Each persistence unit must be identified with a name that is unique to the persistence unit's
scope.
Persistent units can be packaged as part of a WAR or EJB JAR file, or can be packaged as a JAR
file that can then be included in an WAR or EAR file.
If you package the persistent unit as a set of classes in an EJB JAR file, persistence.xml should
be put in the EJB JAR's META-INF directory.
If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be
located in the WAR file's WEB-INF/classes/META-INF directory.
If you package the persistence unit in a JAR file that will be included in a WAR or EAR file, the
JAR file should be located:
In the WEB-INF/lib directory of a WAR.
In the top-level of an EAR file.
In the EAR file's library directory.
The persistence.xml File
persistence.xml
defines one or more persistence units. The following is an example
persistence.xml
file.
<persistence>
<persistence-unit name=
"OrderManagement">
<description>This unit manages orders and customers.
It does not rely on any vendor-specific features and can
therefore be deployed to any persistence provider.
Managing Entities
Chapter 24 · Introduction to the Java Persistence API
701