EntityManager的定義
The EntityManager manages the O/R mapping between a fixed set of entity classes and an underlying data source.
It provides APIs for creating queries, finding objects, synchronizing objects, and inserting objects into the database.
It also can provide caching and manage the interaction between an entity and transactional services in a Java EE environment such as JTA.
The EntityManager is tightly integrated with Java EE and EJB but is not limited to this environment; it can be used in plain Java programs.
An EntityManager maps a fixed set of classes to a particular database. This set of classes is called a persistence unit .
In Java SE, entity managers are created using a javax.persistence.EntityManagerFactory
Example:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("titan", map);
EntityManager manager = factory.createEntityManager();
在Java SE環(huán)境中,使用完EntityManagerFactory后,最好將其關(guān)閉,以釋放其占有的資源。
和Java SE環(huán)境不一樣,在Java EE中,一個(gè)注入的EntityManagerFactory會(huì)被EJB容器自動(dòng)關(guān)閉,實(shí)際上,如果你調(diào)用EntityManagerFactory的clost()方法時(shí),會(huì)拋出IllegalStateException異常。
public interface EntityManager {
public void persist(Object entity);
public <T> T find(Class <T> entityClass, Object primaryKey);
public <T> T getReference(Class <T> entityClass, Object primaryKey);
public <T> T merge(T entity);
public void remove(Object entity);
public void lock(Object entity, LockModeType lockMode);
public void refresh(Object entity);
public boolean contains(Object entity);
public void clear( );
public void joinTransaction( );
public void flush( );
public FlushModeType getFlushMode( );
public void setFlushMode(FlushModeType type);
public Query createQuery(String queryString);
public Query createNamedQuery(String name);
public Query createNativeQuery(String sqlString);
public Query createNativeQuery(String sqlString, String resultSetMapping);
public Query createNativeQuery(String sqlString, Class resultClass);
public Object getDelegate( );
public void close( );
public boolean isOpen( );
}Persistence context的定義
A persistence context is a set of managed entity object instances.
Persistence contexts are managed by an entity manager.
There are two types of persistence contexts: transaction-scoped and extended persistence contexts.
A persistence context can be created by calling the EntityManagerFactory.createEntityManager( ) method. The returned EntityManager instance represents an extended persistence context. If the EntityManagerFactory is JTA-enabled, then you have to explicitly enlist the EntityManager instance within a transaction by calling the EntityManager.joinTransaction( ) method. If you do not enlist the EntityManager within the JTA transaction, then changes you make to your entities are not synchronized with the database.FlushModeType的含義
FlushModeType默認(rèn)為AUTO模式,當(dāng)為AUTO時(shí),在一個(gè)查詢被執(zhí)行前,會(huì)自動(dòng)將變化提交到數(shù)據(jù)庫中,即調(diào)用flush()方法。但是調(diào)用find()或getreference()方法時(shí),并不會(huì)執(zhí)行自動(dòng)提交。當(dāng)為COMMIT模式時(shí),僅僅在事務(wù)提交時(shí),會(huì)將變化提交到數(shù)據(jù)庫中。EJB3中的實(shí)體注解規(guī)范參見如下鏈接
http://wiki.redsaga.com/confluence/display/HART/Home