JPA - Entity Manager

Card Puncher Data Processing


To manage entities in your persistence application, you need to obtain an entity manager from an EntityManagerFactory.

An EntityManager instance is an interface that are used to interact with the persistence context. This API is used to:

  • create persistent entity instances,
  • remove persistent entity instances,
  • find persistent entities by primary key,
  • query over persistent entities.

An entity manager can be acquired through

Type Persistence Environment Life cycle Entity Manager
container-managed J2EE Container
application-managed JSE and J2EE Application

How to acquire an entity manager

in JSE

In the Java SE environment, not the container but the application manages the life cycle of an entity manager.

You must create this entity manager using the EntityManagerFactory's method createEntityManager. You have to use the javax.persistence.Persistence class to bootstrap an EntityManagerFactory instance.

public class Employee {
     public static void main(String[] args) {
         EntityManagerFactory emf =
         EntityManager em = emf.createEntityManager();

Notice that you need to explicitly close the entity manager and the factory.

in J2EE

In the Java EE environment, you acquire an entity manager:


public EntityManager em;

You can only use the @PersistenceContext annotation injection on session beans, servlets and JSP.

  • or using a direct lookup of the entity manager in the JNDI namespace
@PersistenceContext(name="ProjectEM", unitName="Project")
public class ProjectSessionBean implements Project {
     SessionContext ctx;
     public void makeCurrent() {
         try {
            EntityManager em = (EntityManager)ctx.lookup("ProjectEM");


  • through an application-managed entity managers (used in JSE application). You have to create it using the @PersistenceUnit annotation to declare a reference to the EntityManagerFactory for a persistence unit, as the following example shows:
EntityManagerFactory emf;


The getter method does not have a corresponding setter method defined (for a boolean)

Exception Description: The getter method [public boolean generated.ObjectT.isIsDefaultReadable()] on entity class [class generated.ObjectT] does not have a corresponding setter method defined.
	at oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(
	at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(
	at javax.persistence.Persistence.createEntityManagerFactory(
	at org.jvnet.hyperjaxb3.ejb.test.AbstractEntityManagerTest.createEntityManagerFactory(
	at org.jvnet.hyperjaxb3.ejb.test.AbstractEntityManagerTest.setUp(
	at org.jvnet.hyperjaxb3.ejb.test.AbstractEntityManagerSamplesTest.setUp(

Xjc makes use of Boolean in the Set method but it must use the primitive type boolean (without capital b).

A work around is to use a Jaxb binding file with the below node.

<jaxb:globalBindings generateIsSetMethod="true" />

More: Stackoverflow

Documentation / Reference

Discover More
Java Conceptuel Diagram
J2EE - JPA and Stateless Session Bean (EJB) as Data Access Object (DAO)

Stateless EJB session beans as Data Access Object (DAO) implementation with JPA. You must use the EntityManagerFactory to get an EntityManager The resulting EntityManager instance is a PersistenceContext/Cache...
Card Puncher Data Processing
JPA - API (Programming Model)

JPA is a specification that deals with object/relational mapping and data persistence between Java and databases. When developing an application, you need to know how to use the following application...
Card Puncher Data Processing
JPA - Database Synchronization

The Database synchronization involves writing to the database any updates to persistent entities and their relationships. The state of persistent entities is (synchronized to|entered into) the database:...
Card Puncher Data Processing
JPA - Detached Entity

A detached entity results from: transaction commit if a transaction-scoped container-managed entity manager is used from transaction rollback from detaching the entity from the persistence context;...
Card Puncher Data Processing
JPA - Entity (Managed Classes)

xmlparserv2.jar Introduction to EclipseLink JPA (ELUG) for more information on the annotation.
Card Puncher Data Processing
JPA - Java Persistence API (JPA)

API The Java Persistence API is the Java API for the management of persistence and object/relational mapping for: Java EE and Java SE environments The Java Persistence consists of: The Java...
Card Puncher Data Processing
JPA - Persistence Unit

the node provider property See chapter 8.1 of the specification
Card Puncher Data Processing
JPA - persistence.xml

The JPA specification requires the use of a persistence.xml file for deployment. The persistence configuration file must be named “META-INF/persistence.xml” in the persistence archive. This file...
J2ee Server
Java - Container

A container is a logical part of a J2EE Server which contains java components Before any component can be executed, it must be assembled. The assembly process involves per logical container: the...

Share this page:
Follow us:
Task Runner