package org.hibernate; import java.io.Serializable; import java.util.Iterator; import org.hibernate.type.Type; public interface Interceptor { // 加载对象之前执行 public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException;假设您实作了SomeInterceptor类别:
// flush 时,如果发现有Dirty data,则执行此方法 public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException;
// 储存对象前执行 public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException;
// 删除对象前执行 public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException;
// 在 flush 前执行 public void preFlush(Iterator entities) throws CallbackException;
// 在 flush 后执行 public void postFlush(Iterator entities) throws CallbackException;
// 判断传入的对象是否为 transient 状态 public Boolean isTransient(Object entity); // flush 前呼叫这个方法判断 Dirty data // 传回Dirty data属性索引或null采预设行为 public int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types);
// 手动建立实体对象,如果传回 null,则使用预设的建构方法建立实例 public Object instantiate(String entityName, EntityMode entityMode, Serializable id) throws CallbackException;
// 传回实体名称 public String getEntityName(Object object) throws CallbackException;
// 取得实体对象 public Object getEntity(String entityName, Serializable id) throws CallbackException;
// beginTransaction() 之后执行 public void afterTransactionBegin(Transaction tx);
// 在事务完成前执行 public void beforeTransactionCompletion(Transaction tx);
// 在事务完成后执行 public void afterTransactionCompletion(Transaction tx); }
package onlyfun.caterpillar; .... public class SomeInterceptor implements Interceptor { .... }在开启Session时,可以如下加载自订的Interceptor:
SomeInterceptor someInterceptor = new SomeInterceptor(); Session session = sessionFactory.openSession(someInterceptor); ....