Hibernate's interceptor (Interceptor)

Interceptor (Interceptor) The
org.hibernate.Interceptor interface defines the general interception mechanism in Hibernate. When a Session object is
created , all Session objects or all persistent operations of the Session object will be intercepted by the specified interceptor.


Interceptor interface The method

    afterTransactionBegin()
    will call this method immediately when a transaction is started. This method can change the state of the transaction, for example: rollback the transaction
    instantiate()
    to create an object. If it returns null, Hibernate will call the default of the entity class. The constructor creates a persistent object
    getEntity()
    When a persistent object is searched in the cache of the Session object through the identifier property, and it is not found, the method
    getEntityName() is called
    when the session object obtains the name of the persistent object , this method
    onLoad() will be called . This method
    is loaded before the persistent object is initialized. The persistent object is in the state that has just been created (the property values ​​of the object are not assigned)
    findDirty()
    When the flush() method of the Session object is called When calling this method to judge whether the object is dirty data, this is another intercepted implementation of dirty data check
    isTransient()
    When the saveOrUpdate method of the Session object is called, this method is called to determine whether the object has not been saved
    . OnSave()
    is called before the object is saved. This method can be used to modify the properties of the object to be kept.
    OnDelete()
    This method is used in the persistent object. The
    preFlush() method is called before it is deleted.     OnFlushDirty ()
    is called before the flush() method of the Session object is called .     When the flush() method of the Session object is called to check the dirty data, if it is found that the state of the persistent object has changed, it will Call this method     postFlush()     This method is called after calling the flush() method of the Session object.     BeforeTransactionCompletion() is called     before completing a transaction. This method can change the state of the transaction, such as rolling back the transaction     afterTransactionCompletion()     when a transaction is completed. After the transaction, call this method immediately 1. Use the interceptor to implement the operation log In the application system, record all database operations, record the operation content, the user of the operation and the time of the operation Java code Collection code     import org.hibernate .CallbackException; 
















    import org.hibernate.EmptyInterceptor; 
    import org.hibernate.type.Type; 
     
    import java.io.Serializable; 
    import java.util.Date; 
     
     
    public class DaoLogInterceptor extends EmptyInterceptor { 
     
        private static final long serialVersionUID = 1L; 
     
        @Override 
        public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { 
            super.onDelete(entity, id, state, propertyNames, types); 
        } 
     
        @Override 
        public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { 
            return super.onSave(entity, id, state, propertyNames, types); 
        } 
     
        @Override 
        public void onCollectionUpdate(Object collection, Serializable key) throws CallbackException { 
            super.onCollectionUpdate(collection, key); 
        } 
     
        @Override 
        public void onCollectionRemove(Object collection, Serializable key) throws CallbackException { 
            super.onCollectionRemove(collection, key); 
        } 
     
        @Override 
        public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { 
            return super.onLoad(entity, id, state, propertyNames, types); 
        } 
     
        @Override 
        public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { 
            System.out.println("operateTm:"+entity.getClass().getName()+"--OperateTm:"+new Date()+"--操作用户:当前登录用户"); 
            getOperateDetails(previousState, currentState, propertyNames); 
            return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types); 
        } 
     
     
        /**
         * 操作前后的数据差异
         *
         * @param oldState
         * @param state
         * @param operateLog
         * @return
         */ 
        private void getOperateDetails(Object[] oldState, Object[] state, String[] propertyNames) { 
            for (int i = 0; i < propertyNames.length; i++) { 
                if (!equalsObject(oldState[i], state[i])) { 
                    System.out.println("Field:"+propertyNames[i]+"NewValue:"+state[i] +"OldValue:"+oldState[i]); 
                } 
            } 
        } 
     
        private boolean equalsObject(Object o1, Object o2) { 
            if (o1 == null && o2 != null) { 
                return false; 
            } 
            if (o2 == null && o1 != null) { 
                return false; 
            } 
            if (o1 == null && o2 == null) { 
                return true; 
            } 
            return o1.equals(o2); 
        } 
    } 






Xml代码  收藏代码

    <!--hibernate配置--> 
     
        <bean id="myInterceptor" class="com.sf.sfbuy2.context.filter.DaoLogInterceptor"/> 
        <bean id="postUpdateListener" class="com.sf.sfbuy2.context.filter.PostUpdateListener"></bean> 
        <bean id="sessionFactory" 
              class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     
            <property name="dataSource"> 
                <ref bean="dataSource"/> 
            </property> 
            <property name="entityInterceptor"> 
                <ref bean="myInterceptor" /> 
            </property> 
            <property name="hibernateProperties"> 
                <props> 
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
                    <prop key="hibernate.show_sql">true</prop> 
                    <prop key="hibernate.format_sql">true</prop> 
                    <prop key="use_sql_comments">true</prop> 
                    <prop key="javax.persistence.validation.mode">none</prop> 
                    <prop key="hibernate.validator.apply_to_ddl">false</prop> 
                    <prop key="hibernate.validator.autoregister_listeners">false</prop> 
     
                </props> 
            </property> 
            <property name="packagesToScan"> 
                <list> 
                    <value>com.sf.sfbuy2.db.entity</value> 
                </list> 
            </property> 
        </bean>  the event and corresponding listener interface in Hibernate You can use Hibernate's interceptor implementation, or you can use Hibernate's event listener to implementin Hibernate can monitor the actions of the Session object. Once a special event occurs, Hibernate will execute the event processing method in

2. Hibernate's event monitoring mechanism The event monitoring mechanism











                 Event type Listener interface

                     auto-flush AutoFlushEventListener
                        merge MergeEventListener
                        deleteEventListener
                        persist PersistEventListener
                    dirty-check DirtyCheckEventListener
                         evice EvictEventListener
                          flush                                                        FlushEventListener
                    flush-entity                                                    FlushEntityEventListener
                          load                                                         LoadEventListener
                 load-collection                                                  InitializeCollectEventListener
                          lock                                                          LockEventListener
                      refresh                                                         RefreshEventListener
                      replicate                                                      ReplicateEventListener
                     save-update                                                 SaveOrUpdateEventListener
                     pre-load                                                        PreLoadEventListener
                     pre-update                                                    PreUpdateEventListener
                     pre-delete                                                     PreDeleteEventListener
                     pre-insert                                                      PreInsertEventListener
                     post-load                                                       PostLoadEventListener
                     post-update                                                   PostUpdateEventListener
                     post-delete PostDeleteEventListener
                     post-insert PostInsertEventListener




applies Hibernate event listeners

User -defined event listeners first need to implement the interface corresponding to the event to be processed, or inherit the class that implements this interface

by using the Hibernate configuration file (hibernate.cfg. xml) configure the event listener object, or use the Configuration object to register the custom event listener object

LogPostLoadEventListener


Java code collection code

    import org.hibernate.event.PostLoadEvent; 
    import org.hibernate.event.PostLoadEventListener; 
    public class LogPostLoadEventListener implements PostLoadEventListener { 
        private static final long serialVersionUID = 404241098418965422L; 
        public void onPostLoad(PostLoadEvent event) { 
            System.out.println("Class:" + event.getEntity().getClass().getName() + ",id:" 
                    + event.getId()); 
        } 
    } 

Modify Hibernate .cfg.xml file
Xml code Collection code

    <mapping resource="com/rbh/examples/Guestbook.hbm.xml" /> 
            <listener type="post-load" class="com.rbh.examples.LogPostLoadEventListener" /> 
        </session-factory> 
    </hibernate-configuration> 

or register this listener object
Java code through the Configuration object Collection code

    Configuration config = new Configuration(); 
    config.setListener("post-load",  new LogPostLoadEventListener()); 
    config.configure(); 
    Session session = config.buildSessionFactory().getCurrentSession(); 



After writing and configuring the listener, when the persistent object is loaded through the load(), get() methods of the Session object or the list method of the Query object After that, the onPostLoad() method in the LogPostEventListener object will be executed.



Using the listener to record the operation log

Using Hibernate's event mechanism, not only can the modification of the fields of the persistent object, the change of the relationship between the persistent objects, but also be accurately tracked. Can record the value before the update and the value after the



update Comparison between the listener and

the interceptor The listener can achieve more fine-grained
interception Obtain the modified and pre-modified state value of the intercepted persistent object through the listener
Can directly pass The Event object gets the Session object

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529667&siteId=291194637