2.3. Java Persistence API Architecture
The diagram below illustrates the relationships between the primary components of the JPA architecture.
|
|
A number of the depicted interfaces are only required outside of an EJB3-compliant application server.
In an application server, |
-
Persistence: Thejakarta.persistence.Persistenceclass contains static helper methods to obtainEntityManagerFactoryinstances in a vendor-neutral fashion. -
EntityManagerFactory: Thejakarta.persistence.EntityManagerFactoryclass is a factory forEntityManagers. -
EntityManager: Thejakarta.persistence.EntityManageris the primary JPA interface used by applications. EachEntityManagermanages a set of persistent objects, and has APIs to insert new objects and delete existing ones. When used outside the container, there is a one-to-one relationship between anEntityManagerand anEntityTransaction.EntityManagers also act as factories forQueryinstances. -
Entity: Entities are persistent objects that represent datastore records. -
EntityTransaction: EachEntityManagerhas a one-to-one relation with a singlejakarta.persistence.EntityTransaction.EntityTransactions allow operations on persistent data to be grouped into units of work that either completely succeed or completely fail, leaving the datastore in its original state. These all-or-nothing operations are important for maintaining data integrity. -
Query: Thejakarta.persistence.Queryinterface is implemented by each JPA vendor to find persistent objects that meet certain criteria. JPA standardizes support for queries using both the Java Persistence Query Language (JPQL) and the Structured Query Language (SQL). You obtainQueryinstances from anEntityManager.
The example below illustrates how the JPA interfaces interact to execute a JPQL query and update persistent objects. The example assumes execution outside a container.
// get an EntityManagerFactory using the Persistence class
// It is not recommended to obtain a factory often, as construction of a
// factory is a costly operation. Typically you will like to cache
// a factory and then refer it for repeated use
EntityManagerFactory factory = Persistence.createEntityManagerFactory(null);
// get an EntityManager from the factory
EntityManager em = factory.createEntityManager();
// Begin a transaction
em.getTransaction().begin();
// query for all employees who work in our research division
// and put in over 40 hours a week average
Query query = em.createQuery("SELECT e " +
" FROM Employee e " +
" WHERE e.division.name = 'Research' " +
" AND e.avgHours > 40");
List results = query.getResultList();
// give all those hard-working employees a raise
for (Object res : results) {
Employee emp = (Employee) res;
emp.setSalary(emp.getSalary() * 1.1);
}
// commit will detect all updated entities and save them in database
em.getTransaction().commit();
// free the resources
em.close();
Within a container, the EntityManager will be injected and transactions will be handled declaratively.
Thus, the in-container version of the example consists entirely of business logic:
// query for all employees who work in our research division
// and put in over 40 hours a week average - note that the EntityManager em
// is injected using a @Resource annotation
Query query = em.createQuery("select e from Employee e where "
+ "e.division.name = 'Research' and e.avgHours > 40");
List results = query.getResultList();
// give all those hard-working employees a raise
for (Object res : results) {
emp = (Employee) res;
emp.setSalary(emp.getSalary() * 1.1);
}
The remainder of this document explores the JPA interfaces in detail. We present them in roughly the order that you will use them as you develop your application.