Standardizing Java Persistence with the EJB3 Java Persistence API

Standardizing Java Persistence with the EJB3 Java Persistence API

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:

 @PersistenceContext(unitName="onjava")

  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:

  @PersistenceUnit(unitName="onjava")

  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.

Method

Purpose

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:

@PersistenceContext(unitName="onjava")

private EntityManager em; 

... 

Contractor pte = new Contractor(); 

pte.setName("Nistha")

pte.setHourlyRate(new Double(100.0)); 

em.persist(pte);

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.

Prev  [1] [2] [3] [4] [5] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java: None
  • Now: Tutorial for Web and Software Design > Java > JavaBeans > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction