The Java™ Persistence
API represents a simplification
of the persistence programming model.
Data persistence, the ability
to maintain data between application sessions
in some form of nonvolatile storage (such as a relational database),
is crucial
to enterprise applications. Applications that are developed for this
environment
must either manage data persistence themselves or make use of third-party
solutions to handle database updates and retrievals. JPA provides
a mechanism
for managing data persistence and object-relational mapping and functions
for the EJB 3.0 specifications.
JPA is based on the Java programming model that applies to Java EE
environments, but JPA can also function within the Java SE
environment. The JPA specification
defines the object-relational mapping internally, rather than relying
on vendor-specific
mapping implementations, and uses either annotations or XML to map
objects
into database tables.
JPA is designed to operate both inside
and outside of a Java Enterprise
Edition (Java EE) container. When you run
JPA inside a container,
applications can use the container to manage the persistence. If there
is
no container to manage JPA, the application must handle the persistence
management
itself. Applications designed for container managed persistence cannot
be
used outside a container, while applications that manage their own
persistence
can function either in a container environment or a Java SE
environment.
JPA also provides a query language - JPQL - that
you can use to retrieve
objects without writing SQL queries specific to the database you are
working
with.
Java EE
containers that support JPA must supply a persistence provider. A
JPA persistence
provider uses the following elements to persist data in an EJB 3.0
environment:
- Entity objects: An entity is a simple Java class that represents a row in a database
table. Entities can be concrete classes or abstract classes. They
maintain
states by using properties or fields.
- EntityManager:
An EntityManager object maintains the active collection
of entity objects that are being used by the application. The EntityManager
object handles the database interaction and metadata for object-relational
mappings. An instance of an EntityManager object represents a persistence
context. An application in a container can obtain the EntityManager
either
through injection into the application or by looking it up in the Java component
namespace. If the application manages its persistence, the EntityManager
is
obtained from the EntityManagerFactory. The application server containers
typically supply this function, but the EntityManagerFactory is required
if
you are using JPA application-managed persistence.
Note: Injection
of the
EntityManager is only supported for the following artifacts:
- EJB
3.0 session beans.
- EJB 3.0 message-driven beans.
- Servlets,
but injection is not supported in JSPs.
- The main class of
the application client.
- EntityManagerFactory:
The factory is used to create an EntityManager
for database interactions.
- Persistence unit: A persistence
unit consists of the declarative
metadata that describes the relationship of entity class objects to
a relational
database. The EntityManagerFactory uses this data to create a persistence
context that can be accessed through the EntityManager.
- Persistence
context: The persistence context is the set of active
instances that the application is currently handling. The persistence
context
can be created manually or through injection.