ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
# 12.1\. 拦截器(Interceptors) `Interceptor`接口提供了从会话(session)回调(callback)应用程序(application)的机制, 这种回调机制可以允许应用程序在持久化对象被保存、更新、删除或是加载之前,检查并(或)修改其 属性。一个可能的用途,就是用来跟踪审核(auditing)信息。例如:下面的这个`拦截器`,会在一个实现了 `Auditable`接口的对象被创建时自动地设置`createTimestamp`属性,并在实现了 `Auditable`接口的对象被更新时,同步更新`lastUpdateTimestamp`属性。 你可以直接实现`Interceptor`接口,也可以(最好)继承自`EmptyInterceptor`。 ``` package org.hibernate.test; import java.io.Serializable; import java.util.Date; import java.util.Iterator; import org.hibernate.EmptyInterceptor; import org.hibernate.Transaction; import org.hibernate.type.Type; public class AuditInterceptor 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 Auditable ) { updates++; for ( int i=0; i < propertyNames.length; i++ ) { if ( "lastUpdateTimestamp".equals( propertyNames[i] ) ) { currentState[i] = new Date(); return true; } } } return false; } public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { if ( entity instanceof Auditable ) { loads++; } return false; } public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { if ( entity instanceof Auditable ) { creates++; for ( int i=0; i<propertyNames.length; i++ ) { if ( "createTimestamp".equals( propertyNames[i] ) ) { state[i] = new Date(); 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; } } ``` 拦截器可以有两种:`Session`范围内的,和`SessionFactory`范围内的。 当使用某个重载的SessionFactory.openSession()使用`Interceptor`作为参数调用打开一个session的时候,就指定了`Session`范围内的拦截器。 ``` Session session = sf.openSession( new AuditInterceptor() ); ``` `SessionFactory`范围内的拦截器要通过`Configuration`中注册,而这必须在创建`SessionFactory`之前。在这种情况下,给出的拦截器会被这个`SessionFactory`所打开的所有session使用了;除非session打开时明确指明了使用的拦截器。`SessionFactory`范围内的拦截器,必须是线程安全的,因为多个session可能并发使用这个拦截器,要因此小心不要保存与session相关的状态。 ``` new Configuration().setInterceptor( new AuditInterceptor() ); ```