JPA provides the Entity Manager API (EM), which is the main API for application developers to interact with the database. The EM manages the life cycle and state of entities. It supports create, read, update, and delete (CRUD) operations, finding entities, persisting entities, query execution, and so on.
To obtain a container-managed EM instance, you use the @PersistenceContext annotation to have the EJB container inject an EM instance into the application component using dependency injection:
Syntax
@PersistenceContext public EntityManager em;
To obtain an application-managed EM instance, you must first obtain an entity manager factory instance:
Syntax
@PersistenceUnit EntityManagerFactory emf; EntityManager em = emf.createEntityManager();