当session操作数据库时,可以给session对象加入我们自己实现的Interceptor接口,这样就可以监听session的所有动作.
package com.skymr.hb.dao;
import java.io.Serializable;
import java.util.Iterator;
import org.hibernate.CallbackException;
import org.hibernate.EntityMode;
import org.hibernate.Interceptor;
import org.hibernate.Transaction;
import org.hibernate.type.Type;
public class ListenInterceptor implements Interceptor{
public void afterTransactionBegin(Transaction arg0) {
}
public void afterTransactionCompletion(Transaction arg0) {
}
public void beforeTransactionCompletion(Transaction arg0) {
}
public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2,
Object[] arg3, String[] arg4, Type[] arg5) {
return null;
}
public Object getEntity(String arg0, Serializable arg1)
throws CallbackException {
return null;
}
public String getEntityName(Object arg0) throws CallbackException {
return null;
}
public Object instantiate(String arg0, EntityMode arg1, Serializable arg2)
throws CallbackException {
return null;
}
public Boolean isTransient(Object arg0) {
return null;
}
public void onCollectionRecreate(Object arg0, Serializable arg1)
throws CallbackException {
}
public void onCollectionRemove(Object arg0, Serializable arg1)
throws CallbackException {
}
public void onCollectionUpdate(Object arg0, Serializable arg1)
throws CallbackException {
}
public void onDelete(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
}
public boolean onFlushDirty(Object arg0, Serializable arg1, Object[] arg2,
Object[] arg3, String[] arg4, Type[] arg5) throws CallbackException {
return false;
}
public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
return false;
}
public String onPrepareStatement(String arg0) {
return null;
}
public boolean onSave(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
return false;
}
public void postFlush(Iterator arg0) throws CallbackException {
}
public void preFlush(Iterator arg0) throws CallbackException {
}
}
public static Session getSession(){
return sessionFactory.openSession(new Interceptor(){
});
}