锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
鐪嬫潵瀵規柊鎶鏈簡瑙d笉澶燂紝鍙【浜嗙湅EJB3.0鐨勮鑼冧簡
鎰熻Hibernate3.2鍏ㄩ潰鏀寔EJB3.0鐨勮鑼冨浜嶦JB3鐨勬帹騫挎潵璇存槸浠跺ソ浜嬶紝鑷沖皯鍦∣RM榪欐柟闈紝涓鑸▼搴忓憳縐瘡鐨勭粡楠屽彲浠ュ緢蹇熺殑閫傚簲EJB3.0鐨勫紑鍙戙傚彧瑕佺戶緇姞寮哄EJB瀹瑰櫒銆佷簨鍔″鐞嗙瓑鏂歸潰鐨勫涔狅紝灝卞彲浠ュEJB3鏈夊叏闈㈠畬鏁寸殑浜嗚В銆傛瘮璧蜂互鍓岴JB2.1鍜屽父鐢ㄦ妧鏈殑鏍兼牸涓嶅叆鑰岃█錛岀湡鐨勬槸寰堝ぇ鐨勮繘姝ヤ簡銆?
]]>EntityManager鐨勫畾涔?br>
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錛?br> EntityManagerFactory factory = Persistence.createEntityManagerFactory("titan", map);
EntityManager manager = factory.createEntityManager();
鍦↗ava SE鐜涓紝浣跨敤瀹孍ntityManagerFactory鍚庯紝鏈濂藉皢鍏跺叧闂紝浠ラ噴鏀懼叾鍗犳湁鐨勮祫婧愩?br>
鍜孞ava SE鐜涓嶄竴鏍鳳紝鍦↗ava EE涓紝涓涓敞鍏ョ殑EntityManagerFactory浼氳EJB瀹瑰櫒鑷姩鍏抽棴錛屽疄闄呬笂錛屽鏋滀綘璋冪敤EntityManagerFactory鐨刢lost()鏂規硶鏃訛紝浼氭姏鍑篒llegalStateException寮傚父銆?
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鐨勫畾涔?br>
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鐨勫惈涔?br>
FlushModeType榛樿涓篈UTO妯″紡錛屽綋涓篈UTO鏃訛紝鍦ㄤ竴涓煡璇㈣鎵ц鍓嶏紝浼氳嚜鍔ㄥ皢鍙樺寲鎻愪氦鍒版暟鎹簱涓紝鍗寵皟鐢╢lush()鏂規硶銆備絾鏄皟鐢╢ind()鎴杇etreference()鏂規硶鏃訛紝騫朵笉浼氭墽琛岃嚜鍔ㄦ彁浜ゃ傚綋涓篊OMMIT妯″紡鏃訛紝浠呬粎鍦ㄤ簨鍔℃彁浜ゆ椂錛屼細灝嗗彉鍖栨彁浜ゅ埌鏁版嵁搴撲腑銆?br>EJB3涓殑瀹炰綋娉ㄨВ瑙勮寖鍙傝濡備笅閾炬帴
http://wiki.redsaga.com/confluence/display/HART/Home