Hibernate攔截器可以有兩種:Session范圍內的,和SessionFactory范圍內的。
(1)當使用某個重載的SessionFactory.openSession()使用Interceptor作為參數調用打開一個session的時候,就指定了Session范圍內的攔截器。
Session session = sf.openSession( new AuditInterceptor() );
(2)SessionFactory范圍內的攔截器要通過Configuration中注冊,而這必須在創(chuàng)建SessionFactory之前。在這種情況下,給出的攔截器會被這個SessionFactory所打開的所有session使用了;除非session打開時明確指明了使用的攔截器。SessionFactory范圍內的攔截器,必須是線程安全的,因為多個session可能并發(fā)使用這個攔截器,要因此小心不要保存與session相關的狀態(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 -- 屬性對應的值
test:
Session session = sf.openSession(new UserInterceptor());
session.load(class, id);
session.save(obj);