hibernate 中的攔截器EmptyInterceptor接口功能

Interceptor接口提供了從會話(session)回調(callback)應用程序(application)的機制, 這種回調機制能夠容許應用程序在持久化對象被保存、更新、刪除或是加載以前,檢查並(或)修改其 屬性。一個可能的用途,就是用來跟蹤審覈(auditing)信息。例如:下面的這個攔截器,會在一個實現了 Auditable接口的對象被建立時自動地設置createTimestamp屬性,並在實現了 Auditable接口的對象被更新時,同步更新lastUpdateTimestamp屬性。 java

 

你能夠直接實現Interceptor接口,也能夠(最好)繼承自EmptyInterceptor。 web

 

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範圍內的。 spring

 

當使用某個重載的SessionFactory.openSession()使用Interceptor做爲參數調用打開一個session的時候,就指定了Session範圍內的攔截器。 sql

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

或在配置文件中加:app

<bean id="sessionFactory" class="cn.sh.cares.framework.spring.annotationSessionFactoryBean">
<property name="dataSource" ref="ppcDataSource"/>
<!--<property name="lobHandler" ref="lobHandler"/>-->
<property name="annotatedClassesLocations">
<list>
<value>classpath*:</value>
</list>
</property>
<property name="hbmLocations">
<list>
<value>classpath*:</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">cn.sh.cares.framework.dao.hibernate.OracleDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.jdbc.fetch_size">20</prop>
</props>
</property>
<property name="entityInterceptor">
<bean id ="auditInterceptor" class="cn.sh.cares.framework.dao.hibernate.AuditInterceptor" />
</property>
相關文章
相關標籤/搜索