Hibernate攔截器可以有兩種:Session范圍內(nèi)的,和SessionFactory范圍內(nèi)的。
(1)當(dāng)使用某個(gè)重載的SessionFactory.openSession()使用Interceptor作為參數(shù)調(diào)用打開一個(gè)session的時(shí)候,就指定了Session范圍內(nèi)的攔截器。
Session session = sf.openSession( new AuditInterceptor() );
(2)SessionFactory范圍內(nèi)的攔截器要通過(guò)Configuration中注冊(cè),而這必須在創(chuàng)建SessionFactory之前。在這種情況下,給出的攔截器會(huì)被這個(gè)SessionFactory所打開的所有session使用了;除非session打開時(shí)明確指明了使用的攔截器。SessionFactory范圍內(nèi)的攔截器,必須是線程安全的,因?yàn)槎鄠€(gè)session可能并發(fā)使用這個(gè)攔截器,要因此小心不要保存與session相關(guān)的狀態(tài)。
new Configuration().setInterceptor( new AuditInterceptor() );
package com.jason.interceptor;
import java.io.Serializable;
import org.hibernate.EmptyInterceptor;
import org.hibernate.Transaction;
import org.hibernate.type.Type;
public class UserInterceptor extends EmptyInterceptor {
??? private int updates;
??? private int creates;
??? private int loads;
??? public void onDelete(Object entity,
???????????????????????? Serializable id,
???????????????????????? Object[] state,
???????????????????????? String[] propertyNames,
???????????????????????? Type[] types) {
??????? // do nothing
??? }
??? public boolean onFlushDirty(Object entity,
??????????????????????????????? Serializable id,
??????????????????????????????? Object[] currentState,
??????????????????????????????? Object[] previousState,
??????????????????????????????? String[] propertyNames,
??????????????????????????????? Type[] types) {
??????? if ( entity instanceof User ) {
??????????? updates++;
???????????
??????? }
??????? return false;
??? }
??? public boolean onLoad(Object entity,
????????????????????????? Serializable id,
????????????????????????? Object[] state,
????????????????????????? String[] propertyNames,
????????????????????????? Type[] types) {
??????? if ( entity instanceof User ) {
??????????? loads++;
??????????? for ( int i=0; i<propertyNames.length; i++ ) {
??????????????? if ( "password".equals( propertyNames[i] ) ) {
??????????????? ?String temp = (state[i]).toString();
??????????????????? state[i] = temp.substring(3, temp.length()-3);
??????????????????? return true;
??????????????? }
??????????? }
??????? }
??????? return false;
??? }
??? public boolean onSave(Object entity,
????????????????????????? Serializable id,
????????????????????????? Object[] state,
????????????????????????? String[] propertyNames,
????????????????????????? Type[] types) {
??????? if ( entity instanceof User ) {
??????????? creates++;
??????????? for ( int i=0; i<propertyNames.length; i++ ) {
??????????????? if ( "password".equals( propertyNames[i] ) ) {
??????????????????? state[i] = "abc" +state[i] + "xyz";
??????????????????? return true;
??????????????? }
??????????? }
??????? }
??????? return false;
??? }
??? public void afterTransactionCompletion(Transaction tx) {
??????? if ( tx.wasCommitted() ) {
??????????? System.out.println("Creations: "
+ creates + ", Updates: " + updates + ",Loads: " + loads);
??????? }
??????? updates=0;
??????? creates=0;
??????? loads=0;
??? }
}
【注】String[] propertyNames -- 屬性名稱
Object[] state -- 屬性對(duì)應(yīng)的值
test:
Session session = sf.openSession(new UserInterceptor());
session.load(class, id);
session.save(obj);