站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Hibernate 2.1.8 正式版 API 英文文档

Interceptor (Hibernate API Documentation) - Hibernate 2.1.8 正式版 API 英文文档


net.sf.hibernate
Interface Interceptor


public interface Interceptor

Allows user code to inspect and/or change property values.

Inspection occurs before property values are written and after they are read from the database.

There might be a single instance of Interceptor for a SessionFactory, or a new instance might be specified for each Session. Whichever approach is used, the interceptor must be serializable if the Session is to be serializable. This means that SessionFactory-scoped interceptors should implement readResolve().

The Session may not be invoked from a callback (nor may a callback cause a collection or proxy to be lazily initialized).

Author:
Gavin King
See Also:
SessionFactory.openSession(Interceptor), Configuration.setInterceptor(Interceptor)

Method Summary
 int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)
          Called from flush().
 Object instantiate(Class clazz, Serializable id)
          Instantiate the entity class.
 Boolean isUnsaved(Object entity)
          Called when a transient entity is passed to saveOrUpdate().
 void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
          Called before an object is deleted.
 boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)
          Called when an object is detected to be dirty, during a flush.
 boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
          Called just before an object is initialized.
 boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
          Called before an object is saved.
 void postFlush(Iterator entities)
          Called after a flush that actually ends in execution of the SQL statements required to synchronize in-memory state with the database.
 void preFlush(Iterator entities)
          Called before a flush
 

Method Detail

onLoad

public boolean onLoad(Object entity,
                      Serializable id,
                      Object[] state,
                      String[] propertyNames,
                      Type[] types)
               throws CallbackException
Called just before an object is initialized. The interceptor may change the state, which will be propagated to the persistent object. Note that when this method is called, entity will be an empty uninitialized instance of the class.

Returns:
true if the user modified the state in any way.
Throws:
CallbackException

onFlushDirty

public boolean onFlushDirty(Object entity,
                            Serializable id,
                            Object[] currentState,
                            Object[] previousState,
                            String[] propertyNames,
                            Type[] types)
                     throws CallbackException
Called when an object is detected to be dirty, during a flush. The interceptor may modify the detected currentState, which will be propagated to both the database and the persistent object. Note that not all flushes end in actual synchronization with the database, in which case the new currentState will be propagated to the object, but not necessarily (immediately) to the database. It is strongly recommended that the interceptor not modify the previousState.

Returns:
true if the user modified the currentState in any way.
Throws:
CallbackException

onSave

public boolean onSave(Object entity,
                      Serializable id,
                      Object[] state,
                      String[] propertyNames,
                      Type[] types)
               throws CallbackException
Called before an object is saved. The interceptor may modify the state, which will be used for the SQL INSERT and propagated to the persistent object.

Returns:
true if the user modified the state in any way.
Throws:
CallbackException

onDelete

public void onDelete(Object entity,
                     Serializable id,
                     Object[] state,
                     String[] propertyNames,
                     Type[] types)
              throws CallbackException
Called before an object is deleted. It is not recommended that the interceptor modify the state.

Throws:
CallbackException

preFlush

public void preFlush(Iterator entities)
              throws CallbackException
Called before a flush

Throws:
CallbackException

postFlush

public void postFlush(Iterator entities)
               throws CallbackException
Called after a flush that actually ends in execution of the SQL statements required to synchronize in-memory state with the database.

Throws:
CallbackException

isUnsaved

public Boolean isUnsaved(Object entity)
Called when a transient entity is passed to saveOrUpdate(). The return value determines
  • Boolean.TRUE - the entity is passed to save(), resulting in an INSERT
  • Boolean.FALSE - the entity is passed to update(), resulting in an UPDATE
  • null - Hibernate uses the unsaved-value mapping to determine if the object is unsaved

Parameters:
entity - a transient entity
Returns:
Boolean or null to choose default behaviour

findDirty

public int[] findDirty(Object entity,
                       Serializable id,
                       Object[] currentState,
                       Object[] previousState,
                       String[] propertyNames,
                       Type[] types)
Called from flush(). The return value determines whether the entity is updated
  • an array of property indices - the entity is dirty
  • and empty array - the entity is not dirty
  • null - use Hibernate's default dirty-checking algorithm

Parameters:
entity - a persistent entity
Returns:
array of dirty property indices or null to choose default behaviour

instantiate

public Object instantiate(Class clazz,
                          Serializable id)
                   throws CallbackException
Instantiate the entity class. Return null to indicate that Hibernate should use the default constructor of the class. The identifier property of the returned instance should be initialized with the given identifier.

Parameters:
clazz - a mapped class
id - the identifier of the new instance
Returns:
an instance of the class, or null to choose default behaviour
Throws:
CallbackException