随笔-75  评论-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环境中,使用完EntityManagerFactory后,最好将其关闭,以释放其占有的资源。

和Java SE环境不一样,在Java EE中,一个注入的EntityManagerFactory会被EJB容器自动关闭,实际上,如果你调用EntityManagerFactory的clost()方法时,会抛出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默认为AUTO模式,当为AUTO时,在一个查询被执行前,会自动将变化提交到数据库中,即调用flush()方法。但是调用find()或getreference()方法时,并不会执行自动提交。当为COMMIT模式时,仅仅在事务提交时,会将变化提交到数据库中。

EJB3中的实体注解规范参见如下链接
http://wiki.redsaga.com/confluence/display/HART/Home

posted on 2007-05-07 23:42 The Matrix 阅读(560) 评论(0)  编辑  收藏 所属分类: EJB3

只有注册用户登录后才能发表评论。


网站导航: