Hibernate拦截器可以有两种:Session范围内的,和SessionFactory范围内的。

(1)当使用某个重载的SessionFactory.openSession()使用Interceptor作为参数调用打开一个session的时候,就指定了Session范围内的拦截器。

Session session = sf.openSession( new AuditInterceptor() );

(2)SessionFactory范围内的拦截器要通过Configuration中注册,而这必须在创建SessionFactory之前。在这种情况下,给出的拦截器会被这个SessionFactory所打开的所有session使用了;除非session打开时明确指明了使用的拦截器。SessionFactory范围内的拦截器,必须是线程安全的,因为多个session可能并发使用这个拦截器,要因此小心不要保存与session相关的状态。

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);