首页 / 软件开发 / JAVA / hibernate3学习笔记(二十三)|进阶特性(二)
hibernate3学习笔记(二十三)|进阶特性(二)2011-02-034.Interceptor 介面:您可以在开启Session时载入一个自订Interceptor,这个Interceptor会在对应的动作发生之前呼叫对应的方法,方法是让您定义的Interceptor实作Interceptor介面,介面的定义如下:Interceptor.java
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;
// 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);
}