Chapter 3. EJB Persistence Architecture
Prev | Part 2. Java Persistence API | Next

## Chapter 3. EJB Persistence Architecture

The diagram below illustrates the relationships between the primary components of the EJB architecture.

| Note
A number of the depicted interfaces are only required outside of an EJB3-compliant application server. In an application server, `EntityManager` instances are typically injected, rendering the `EntityManagerFactory` unnecessary. Also, transactions within an application server are handled using standard application server transaction controls. Thus, the `EntityTransaction` also goes unused.

- `Persistence`: The `javax.persistence.Persistence` class contains static helper methods to obtain `EntityManagerFactory` instances in a vendor-neutral fashion.

- `EntityManagerFactory` : The `javax.persistence.EntityManagerFactory` class is a factory for `EntityManager`s.

- `EntityManager`: The `javax.persistence.EntityManager` is the primary EJB persistence interface used by applications. Each `EntityManager` manages 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 an `EntityManager` and an `EntityTransaction`. `EntityManager`s also act as factories for `Query` instances.

- `Entity`: Entites are persistent objects that represent datastore records.

- `EntityTransaction`: Each `EntityManager` has a one-to-one relation with a single `javax.persistence.EntityTransaction`. `EntityTransaction`s 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`: The `javax.persistence.Query` interface is implemented by each EJB vendor to find persistent objects that meet certain criteria. EJB standardizes support for queries using both the EJB Query Language (JPQL) and the Structured Query Language (SQL). You obtain `Query` instances from an `EntityManager`.

The example below illustrates how the EJB interfaces interact to execute a JPQL query and update persistent objects. The example assumes execution outside a container.

Example 3.1. Interaction of Interfaces Outside Container

    // get an EntityManagerFactory using the Persistence class; typically
    // the factory is cached for easy repeated use
    EntityManagerFactory factory = Persistence.createEntityManagerFactory (null);

    // get an EntityManager from the factory
    EntityManager em = factory.createEntityManager (PersistenceContextType.EXTENDED);

    // updates take place within transactions
    EntityTransaction tx = em.getTransaction ();
    tx.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 the updates and free resources
    tx.commit ();
    em.close ();
    factory.close ();

Within a container, the `EntityManager` will be injected and transactional handled declaratively. Thus, the in-container version of the example consists entirely of business logic:

Example 3.2. Interaction of Interfaces Inside Container

    // 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 EJB interfaces in detail. We present them in roughly the order that you will use them as you develop your application.

## 3.1. EJB Exceptions

The diagram above depicts the EJB persistence exception architecture. All exceptions are unchecked. EJB persistence uses standard exceptions where appropriate, most notably `IllegalArgumentException`s and `IllegalStateException`s. The specification also provides a few EJB-specific exceptions in the `javax.persistence` package. These exceptions should be self-explanatory. See the Javadoc for additional details on EJB exceptions.

| Note
All exceptions thrown by Kodo implement `kodo.util.ExceptionInfo` to provide you with additional error information.

Prev | Up | Next
Chapter 2. Why JPA? | Home | Chapter 4. Entity