ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Standardizing Java Persistence with the EJB3 Java Persistence API
Pages: 1, 2, 3, 4, 5

The Entity Manager API: Standard API for Entity operations

The javax.persistence.EntityManager manages entity lifecycles and exposes several methods to perform CRUD operations on entities.

The EntityManager API is invoked in a transaction context. You can invoke it outside of the EJB container--for instance, from a Web application--without needing a session bean façade.

You must grab an instance of EntityManager before you can perform any entity operation. You can use either a container-managed or an application-managed entity manager, and you can use either JNDI lookup or dependency injection to grab an instance of EntityManager. As the name suggests, the Java EE container manages the lifecycle of a container-managed entity manager. It's probably used most in enterprise Java applications.

You can grab an instance of a container-managed entity manager by using the PersistenceContext injection, as follows:

  private EntityManager em;

If you use an application-managed entity manager, you must manage its lifecycle. You can create an instance of an application-managed entity manager like this:

  private EntityManagerFactory emf;
  private EntityManager em = emf.createEntityManager();

You can then use the EntityManager instance to perform CRUD operation on entities. To close the instance of an application-managed entity manager, invoke the em.close() method once you're done.

As mentioned earlier, an entity manager operation that involves any database changes must be done in a transactional context.

The following table lists some key methods of the EntityManager interface that are used to perform entity operations.



public void persist(Object entity);

Persist an entity instance.

public <T> T merge(T entity);

Merge a detached entity instance.

public void remove(Object entity);

Remove an entity instance.

public <T> T find(Class<T> entityClass, Object primaryKey);

Retrieve entity instance by its primary key.

public void flush();

Synchronize entity state with database.

You can use the persist() method to persist an instance of entity. For example, if you want to persist an instance of Contractor, use the following code:

private EntityManager em; 
Contractor pte = new Contractor(); 
pte.setHourlyRate(new Double(100.0)); 

If you persist an entity, any state changes to associated entities will be persisted as well if CascadeType for the relationship is set to PERSIST or ALL. Unless you're using an extended persistent context, the entities will become detached after the end of the transaction. The merge operation lets you merge a detached entity instance with the persistent context; the state of a detached entity will be synchronized with the database. This helps you get rid of the Data Transfer Object (DTO) anti-pattern prevalent in EJB 2.x because entities, being POJOs, can be transferred between tiers. The only requirement is that the entity class must implement the java.io.Serializable interface.

Pages: 1, 2, 3, 4, 5

Next Pagearrow