Skip Headers

# Understanding the EJB 3.0 JPA Entity Architecture

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