| Oracle® TopLink Developer's Guide 10g (10.1.3.1.0) B28218-01 |
|
![]() Previous |
![]() Next |
A part of the EJB 3.0 specification, the Java Persistence API (JPA) is a lightweight, POJO-based framework for Java persistence. JPA focuses on object-relational mapping and contains a full object-relational mapping specification supporting the use of Java language metadata annotations and/or XML descriptors to define the mapping between Java objects and a relational database. Object-relational mapping with the JPA is completely metadata-driven. JPA supports a SQL-like query language for both static and dynamic queries. It also supports the use of pluggable persistence providers.
JPA includes the following concepts:
Entity–any application-defined object with the following characteristics can be an entity:
it can be made persistent;
it has a persistent identity (a key that uniquely identifies an entity instance and distinguishes it from other instances of the same entity type. An entity has a persistent identity when there is a representation of it in a data store);
it is partially transactional in a sense that a persistence view of an entity is transactional (an entity is created, updated and deleted within a transaction, and a transaction is required for the changes to be committed in the database). However, in-memory entities can be changed without the changes being persisted.
it is not a primitive, a primitive wrapper, or built-in object. An entity is a fine-graned object that has a set of aggregated state that is typically stored in a single place (such as a row in a table), and have relationships to other entities.
Entity metadata–describes every entity. Metadata could be expressed as annotations (specifically defined types that may be attached to or place in front of Java programming elements) or XML (descriptors).
Entity manager–enables API calls to perform operations on an entity. Until an entity manager is used to create, read, or write an entity, the entity is just a regular nonpersistent Java object. When an entity manager obtains a reference to an entity, that entity becomes managed by the entity manager. The set of managed entity instances within an entity manager at any given time is called its persistence context–only one Java instance with the same persistent identity may exist in a persistence context at any time.
You can configure an entity manager to be able to persist or manage certain types of objects, read or write to a particular database, and be implemented by a specific persistence provider. The persistence provider supplies the backing implementation engine for JPA, including the EntityManager interface implementation, the Query implementation, and the SQL generation.
Entity managers are provided by an EntityManagerFactory. The configuration for an entity manager is bound to the EntityManagerFactory, but it is defined separately as a persistence unit. You name persistence units to allow differentiation between EntityManagerFactory objects. This way your application obtains control over which configuration to use for operations on a specific entity. The configuration that describes the persistence unit is defined in a persistence.xml file.
The following description expresses relationships between JPA concepts:
Persistence creates one or more EntityManagerFactory objects;
each EntityManagerFactory is configured by one persistence unit;
EntityManagerFactory creates one or more EntityManager objects;
one or more EntityManager manages one PersistenceContext.
For more information, see the following:
http://www.oracle.com/technology/products/ias/toplink/jpa/index.html
http://www.oracle.com/technology/products/ias/toplink/jpa/index.html
Oracle Containers for J2EE Enterprise JavaBeans Developer's Guide
An example of the entity beans with bean-managed persistence implementation is a Model-View-Controller Model 2 architectural design pattern that runs in a Java EE container, with servlets and JSP that access session beans and EJB 3.0-compliant entities using the TopLink-based JPA persistence provider.
The use of TopLink JPA entities offers the following advantages:
POJO persistence–in JPA, persistent objects are POJOs.
Object-relational mapping is completely metadata-driven.
The persistence API exists as a separate layer form the persistent objects and does not intrude upon them.
Using the query framework you can query across entities and their relationships without having to use concrete foreign keys or database columns. Also, you can define queries statically in metadata or create them dynamically by passing query criteria on construction. Queries can return entities as results.
Entities are mobile–objects are able to move from one JVM to another and back, and at the same time be usable by the application.
You can configure persistence features through the use of Java SE 5 annotations, or XML, or a combination of both. You may also rely on defaults.
If your application is running inside a container, the container provides support and ease of use; you can configure the same application to run outside a container.