隨筆-75  評(píng)論-193  文章-5  trackbacks-0

          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

          posted on 2007-05-07 23:42 The Matrix 閱讀(601) 評(píng)論(0)  編輯  收藏 所屬分類: EJB3

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 北碚区| 苗栗县| 建阳市| 新龙县| 武鸣县| 洛南县| 云安县| 兰西县| 伊金霍洛旗| 宝丰县| 深水埗区| 安顺市| 土默特左旗| 铜川市| 宁海县| 郴州市| 陆川县| 长顺县| 彭水| 汉沽区| 廊坊市| 长春市| 稷山县| 阿拉尔市| 息烽县| 西林县| 舞阳县| 景德镇市| 鄢陵县| 邢台市| 土默特左旗| 临城县| 庆阳市| 西昌市| 湖北省| 台北县| 齐齐哈尔市| 三穗县| 阿瓦提县| 河源市| 太谷县|