Java Reference
In-Depth Information
The EntityManagerFactory is used within an application to create an entity
manager. An EntityManager instance is created using the EntityManagerFactory
and is associated with a persistence context, which is a collection of persistent entity
instances, each of which have a unique persistent identity. The EntityManager
is used to create and remove persistent entity instances, find entities by their
primary key, and query entities. The collection of entities that can be managed by
an EntityManager instance and that are mapped to a single database is defined
by a persistence unit. A persistence unit is defined in the persistence.xml
configuration file. The EntityManager interface is the main runtime API of the JPA.
An EntityManager can be injected directly into a class using dependency injection
or can be obtained from an EntityManagerFactory instance that has been injected
using dependency injection. The Query interface is used in query execution and is
created from an EntityManager instance. The EntityTransaction interface is used
to manage resource-local transactions. We shall discuss the types of transactions
supported by an entity manager in a subsequent section.
Advantages of JPA
JPA has several advantages over the entity persistence provided in the EJB 2.1
specification. The following are the main advantages:
• Persistent objects are POJOs; as a result fewer classes and interfaces
are required.
• Object relational mapping is simpliied with the use of metadata annotations
(including annotation defaults), which replace the deployment descriptors.
• EJB 3.0 entities can be created and persisted outside the EJB container as
they are not bound to the container via interfaces and container-managed
EJB relationships as EJB 2.x entities were. Only a bean class containing
annotations, which is a POJO, and a persistence.xml configuration file,
which maps the bean class to a database, are required. Using an application-
managed entity manager and resource-local transactions, EJB 3.0 entities can
be created and persisted using only the J2SE 5 API.
• Persistent entity instances represent database rows, and entities and entity
relationships can be queried using the query framework without requiring
reference to foreign keys and database columns.
• Queries may be speciied statically in the metadata or constructed
dynamically. The Java persistence query language is an enhanced EJB-QL
and includes some of the features lacking in EJB-QL.
• JPA can be used with pluggable third-party persistence providers.
• JNDI is replaced with resource injection, also called dependency injection.
Search WWH ::

Custom Search