JPA - Entity Manager


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

Powered by ComboStrap