Show TOC

Procedure documentationObtaining an Entity Manager Instance Locate this document in the navigation structure

 

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.

Procedure

  1. 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 Syntax

    1. @PersistenceContext
      public EntityManager em;
    End of the code.
  2. To obtain an application-managed EM instance, you must first obtain an entity manager factory instance:

    Syntax Syntax

    1. @PersistenceUnit
      EntityManagerFactory emf;
      EntityManager em = emf.createEntityManager();
    End of the code.
More Information

Entity Manager