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
事件是實現對象通訊的一個重要手段,對于構建一個靈活的系統來說是非常必要的。在Flash Player 9中,將事件發送機制內建在了flash.events.EventDispatcher類中。所有需要發送事件的類都必須繼承EventDispatcher類。
調用EventDispatcher類中的addEventListener( ) and removeEventListener( )方法,就可以注冊或者移除事件監聽器。EventDispatcher中還定義了dispatchEvent()方法,可以使用該方法發送事件。dispatchEvent()方法至少需要一個參數,即flash.events.Event對象或者它的子類。
原文如下:
Events are an important way for objects to communicate. They are essential for creating flexible systems. Flash Player 9, for example, has a built-in event dispatching mechanism in the flash.events.EventDispatcher class. All classes that dispatch events inherit from EventDispatcher (e.g., NetStream and Sprite). If you want to define a class that dispatches events, you can extend EventDispatcher, as follows:
package {
import flash.events.EventDispatcher;
public class Example extends EventDispatcher {
}
}
The EventDispatcher class has public methods called addEventListener( ) and removeEventListener( ) that you can call from any instance of an EventDispatcher subclass to register event listeners. EventDispatcher also defines a protected method called dispatchEvent( ), which you can call from within a subclass to dispatch an event. The dispatchEvent( ) method requires at least one parameter as a flash.events.Event object or a subclass of Event.