Ch06 Entity Manager - skatscher/pro_jpa2_book GitHub Wiki

This chapter describes JPA Entity Managers.

Persistence Contexts

  • A persistence unit is a named configuration of entity classes. A persistence context is a managed set of entity instances. Every persistence context is associated with a persistence unit restricting the classes of the managed instances to the set defined by the persistence unit.
  • If the persistence context participates in a transaction, the in-memory state of the managed entities will get synchronized with the database.
  • the persistence context is only accessible through the entity manager

Entity Managers

  • JPA defines 3 types of entity managers that is tailored to a specific application need:
    • Container-managed entity manager
    • Application-managed entity manager

Container-managed Entity Managers

  • In the Java EE environment the most common way to aquire an entity manager is by using the @PersistenceContext annotation to inject one. This is a container-managed entity manager - the application does not have to create or close it.
  • Container-managed entity managers come in two varieties:
    • transaction-scoped
    • extended

Transaction-Scoped

  • the persistence contexts managed by entity manager are scoped by the active JTA transaction
  • a transaction-scoped entity manager is returned whenever the reference created by the @PersistenceContext is resolved
  • the transaction-scoped entity manager is stateless and therefore basically maintenance-free
@Stateless
public class ProjectServiceBean implements ProjectService {
  @PersistenceContext(unitName="EmployeeService")
  EntityManager em;
  //...
}
  • All container-managed entity managers depend on JTA transactions
  • Every time an operation is invoked on the entity manager, the container proxy for that entity manager checks to see whether a persistence context is associated with the JTA transaction. If it does not find one, it creates a new persistence context and associates it with the transaction. When the transaction ends, the persistence context goes away.

Extended

  • Extended entity manager works with a single persistence context that is tied to the lifecycle of a stateful session bean and is scoped to the life of the stateful session bean, potentially spanning multiple transactions.
@Stateful
public class DepartmentManagerBean implements DepartmentManager {
  @PersistenceContext(unitName="EmployeeService", type=PersistenceContextType.EXTENDED)
  EntityManager em;
  // ...
  @Remove
  public void finished() {
  }
}
  • type=PersistenceContextType.EXTENDED defines, that the extended entity manager should be used. Default is TRANSACTION for transaction-scoped.

Application-Managed Entity Manager

  • the application rather the container manages the lifecycle of the EM
  • only EM type available in Java SE, but can be also used in Java EE
  • any EM that is created from the createEntityManager() call of an EntityManagerFactory instance is called application-managed EM

Application-Managed EM in Java SE

EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService");
EntityManager em = emf.createEntityManager();
...
em.close();
emf.close();

Application-Managed EM in Java EE

@PerstistenceUnit(unitName="EmployeeService")
EntityManagerFactory emf;
...
EntityManager em = emf.createEntityManager();
...
em.close;