- Persistence context: A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle is managed by a particular entity manager. The scope of this context can either be the transaction, or an extended unit of work.
EntityManagerFactory
: An entity manager factory provides entity manager instances, all instances are configured to connect to the same database. You can prepare several entity manager factories to access several data stores.
EntityManager
provides
API to create and remove persistent entity instances, to find entities by their primary key identity, and to query over all entities. An Entity Manager whose lifecycle is managed by the container is Container-managed entity manager.
An Entity Manager whose lifecycle is managed by the application is Application-managed entity manager.
Entity manager involved in a JTA transaction is JTA entity manager, generally its also container-managed entity manager.
Entity manager using a resource transaction (not a JTA transaction) is Resource-local entity manager. Generally its Application-managed entty manager.
An application-managed entity manager allows you to control the entity manager in application code. This entity manager is retrieved through the EntityManagerFactory
API. An application managed entity manager can be either involved in the current JTA transaction (using EntityManager.joinTransaction) , or the transaction may be controlled through the
EntityTransaction
API (a resource-local entity manager). The resource-local entity manager transaction maps to a direct resource transaction (i. e. JDBC transaction). The entity manager type (JTA or resource-local) is defined at configuration time, when setting up the entity manager factory.
Persistence context scope can be bounded to a transaction or can be bounded to several transactions(extended transaction scope).
A resource-local entity manager or an entity manager created with
EntityManagerFactory.createEntityManager()
(application-managed) has a one-to-one relationship with a persistence context and is always (in J2Se mode) extended persistence context.