Table of Contents

About

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 =
             Persistence.createEntityManagerFactory("EmpService");
         EntityManager em = emf.createEntityManager();
     ...
         em.close();
         emf.close();
}

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:

container-managed

@PersistenceContext 
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
@Stateless
@PersistenceContext(name="ProjectEM", unitName="Project")
public class ProjectSessionBean implements Project {
     @Resource 
     SessionContext ctx;
 
     public void makeCurrent() {
         try {
            EntityManager em = (EntityManager)ctx.lookup("ProjectEM");
            ...
     }
 }

application-managed

  • 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:
@PersistenceUnit
EntityManagerFactory emf;

Support

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(PersistenceUnitLoadingException.java:143)
	at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:169)
	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
	at org.jvnet.hyperjaxb3.ejb.test.AbstractEntityManagerTest.createEntityManagerFactory(AbstractEntityManagerTest.java:85)
	at org.jvnet.hyperjaxb3.ejb.test.AbstractEntityManagerTest.setUp(AbstractEntityManagerTest.java:44)
	at org.jvnet.hyperjaxb3.ejb.test.AbstractEntityManagerSamplesTest.setUp(AbstractEntityManagerSamplesTest.java:39)

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