Wednesday, December 22, 2010

EntityManager

EntityManager manages entities. The javax.persistence.EntityManager instances represent the entity manager. Each EntityManager instance is associated with a persistent context. A persistence context defines the scope under which particular entity instances are created, persisted, and removed and it is a set of managed entity instances that exist in a particular data source. The EntityManager interface defines the methods that are used to interact with the persistence context like creating and removing persistent entity instances, finding entities by the entity’s primary key, and allowing queries to be run on entities.

Container-Managed Entity Managers


EntityManager instance’s persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction. The Java EE container manages the life cycle of container-managed entity managers.

To obtain an EntityManager instance, inject the entity manager into the application component:

@PersistenceContext
EntityManager em;

Application-Managed Entity Managers

The persistence context is not propagated to application components, and the life cycle of EntityManager instances is managed by the application. Applications create EntityManager instances using the createEntityManager method of javax.persistence.EntityManagerFactory.
To obtain an EntityManager instance, you first must obtain an EntityManagerFactory instance by injecting it into the application component by means of the javax.persistence.PersistenceUnit annotation:

@PersistenceUnit
EntityManagerFactory emf;
EntityManager em = emf.createEntityManager();

The EntityManager.find method is used to look up entities in the data store by the entity’s primary key.

@PersistenceContext
EntityManager em;
public void enterOrder(int custID, Order newOrder) {
    Customer cust = em.find(Customer.class, custID);
    cust.getOrders().add(newOrder);
    newOrder.setCustomer(cust);
}

Managing an Entity Instance’s Life Cycle

The four states of an entity instance are new, managed, detached and removed. New entity instances have no persistent identity and are not yet associated with a persistence context. Managed entity instances have a persistent identity and are associated with a persistence context. Detached entity instances have a persistent identify and are not currently associated with a persistence context. Removed entity instances have a persistent identity, are associated with a persistent context, and are scheduled for removal from the data store.

By invoking the persist method or by a cascading persist operation invoked from related entities that have the cascade=PERSIST or cascade=ALL elements set in the relationship annotation, new entity instances become managed and persistent. The entity’s data is stored to the database when the transaction associated with the persist operation is completed. Managed entity instances are removed by invoking the remove method, or by a cascading remove operation invoked from related entities that have the cascade=REMOVE or cascade=ALL elements set in the relationship annotation.

Creating Queries

The EntityManager.createQuery and EntityManager.createNamedQuery methods are used to query the data store using Java Persistence query language queries. The createQuery method is used to create dynamic queries.

public List findWithName(String name) {
return em.createQuery(
    "SELECT c FROM Customer c WHERE c.name LIKE :custName")
    .setParameter("custName", name)
    .setMaxResults(10)
    .getResultList();
}

The createNamedQuery method is used to create static queries.

@NamedQuery(
    name="findAllCustomersWithName",
    query="SELECT c FROM Customer c WHERE c.name LIKE :custName"
)


@PersistenceContext
public EntityManager em;
...
customers = em.createNamedQuery("findAllCustomersWithName")
    .setParameter("custName", "Smith")
    .getResultList();

No comments:

Post a Comment