8.4.2 Types of EntityManager
The types of EntityManager include the container-managed EntityManager and the application-managed EntityManager. The following is a description of each type:
- Organization of this subsection
(1) Container-managed EntityManager
The method entrusts the creation and destruction of EntityManager to the container. If you use a container-managed EntityManager, you can code an application without being aware of the generation and destruction of EntityManager. The following points describe how to obtain and how to destroy the container-managed EntityManager.
-
How to obtain the container-managed EntityManager
To obtain the container-managed EntityManager, you use the DI or JNDI lookup in the application. EntityManager obtained using this method is EntityManager created by the container. You can use EntityManager obtained from a container as it is during the application coding.
For details on how to obtain the container-managed EntityManager from an application, see 8.6 How to obtain the container-managed EntityManager.
-
How to destroy the container-managed EntityManager
The creation and destruction of EntityManager need not be coded in the application.
(2) Application-managed EntityManager
In this method, the application explicitly creates and destroys EntityManager. The life cycle is managed explicitly by application coding. The following points describe how to obtain and how to destroy the application-managed EntityManager.
-
How to obtain the application-managed EntityManager
You use EntityManagerFactory to create EntityManager in the application. To obtain EntityManagerFactory, you use the DI or JNDI lookup in the application.
For details on how to obtain the application-managed EntityManager, see 8.7 How to obtain the application-managed EntityManager.
-
How to destroy the application-managed EntityManager
You invoke the close method of EntityManager to destroy EntityManager.