Chapter 7.  EntityManagerFactory

Table of Contents

1. Obtaining an EntityManagerFactory
2. Obtaining EntityManagers
3. Persistence Context
3.1. Transaction Persistence Context
3.2. Extended Persistence Context
4. Closing the EntityManagerFactory

The EntityManagerFactory creates EntityManager instances for application use.


OpenJPA extends the standard EntityManagerFactory interface with the OpenJPAEntityManagerFactory to provide additional functionality.

1.  Obtaining an EntityManagerFactory

Within a container, you will typically use injection to access an EntityManagerFactory. There are, however, alternative mechanisms for EntityManagerFactory construction.

Some vendors may supply public constructors for their EntityManagerFactory implementations, but we recommend using the Java Connector Architecture (JCA) in a managed environment, or the Persistence class' createEntityManagerFactory methods in an unmanaged environment, as described in Chapter 6, Persistence . These strategies allow vendors to pool factories, cutting down on resource utilization.

JPA allows you to create and configure an EntityManagerFactory, then store it in a Java Naming and Directory Interface (JNDI) tree for later retrieval and use.