Interceptor Emptyinterceptor interface function in Hibernate

Source: Internet
Author: User

The Interceptor interface provides a mechanism for a session callback (callback) application (application) that allows the application to examine and/or modify its properties before persisting, updating, deleting, or loading the persisted object. One possible use is to track audit (auditing) information. For example: The following interceptor automatically sets the Createtimestamp property when an object that implements the Auditable interface is created and updates the Lastupdatetimestamp property synchronously when the object that implements the Auditable interface is updated.

You can implement the Interceptor interface directly, or you can (preferably) inherit from Emptyinterceptor.

 Packageorg.hibernate.test;Importjava.io.Serializable;Importjava.util.Date;ImportJava.util.Iterator;ImportOrg.hibernate.EmptyInterceptor;Importorg.hibernate.Transaction;ImportOrg.hibernate.type.Type; Public classAuditinterceptorextendsEmptyinterceptor {Private intupdates; Private intcreates; Private intloads;  Public voidOnDelete (Object entity, Serializable ID, object[] state, String[] PropertyNames, type[] types) {// do nothing    }     Public BooleanOnflushdirty (Object entity, Serializable ID, object[] C                                 Urrentstate, object[] previousstate, string[] propertynames, Type[] types) {if(EntityinstanceofAuditable) {Updates++;  for(inti=0; i < propertynames.length; i++ ) {                if("Lastupdatetimestamp". Equals (Propertynames[i])) {Currentstate[i]=NewDate (); return true; }            }        }        return false; }     Public BooleanonLoad (Object entity, Serializable ID, object[] state, String[] PropertyNames, type[] types) {if(EntityinstanceofAuditable) {Loads++; }        return false; }     Public BooleanOnSave (Object entity, Serializable ID, object[] state, String[] PropertyNames, type[] types) {if(EntityinstanceofAuditable) {Creates++;  for(inti=0; i<propertynames.length; i++ ) {                if("Createtimestamp". Equals (Propertynames[i])) {State[i]=NewDate (); return true; }            }        }        return false; }     Public voidaftertransactioncompletion (Transaction tx) {if(tx.wascommitted ()) {System.out.println ("Creations:" + creates + ", updates:" + Updates, "Loads:" +loads); } Updates=0; Creates=0; Loads=0; }}

Interceptors can have two types: within the session range, and within the Sessionfactory range.

When a session is opened with an overloaded sessionfactory.opensession () using interceptor as a parameter, the interceptor within the session range is specified.

New Auditinterceptor ());

Or in the configuration file, add:

<BeanID= "Sessionfactory"class= "Cn.sh.cares.framework.spring.annotationSessionFactoryBean">    < Propertyname= "DataSource"ref= "Ppcdatasource"/>    <!--<property name= "Lobhandler" ref= "Lobhandler"/> -    < Propertyname= "Annotatedclasseslocations">        <List>            <value>classpath*:</value>        </List>    </ Property>
< Propertyname= "Hbmlocations"> <List> <value>classpath*:</value> </List> </ Property> < Propertyname= "Hibernateproperties"> <Props> <propKey= "Hibernate.dialect">Cn.sh.cares.framework.dao.hibernate.OracleDialect</prop> <propKey= "Hibernate.format_sql">True</prop> <propKey= "Hibernate.show_sql">True</prop> <propKey= "Hibernate.use_sql_comments">False</prop> <propKey= "Hibernate.cache.provider_class">Org.hibernate.cache.EhCacheProvider</prop> <propKey= "Hibernate.cache.use_query_cache">True</prop> <propKey= "Hibernate.cache.use_second_level_cache">True</prop> <propKey= "Hibernate.jdbc.batch_size">20</prop> <propKey= "Hibernate.jdbc.fetch_size">20</prop> </Props> </ Property> < Propertyname= "Entityinterceptor"> <BeanID= "Auditinterceptor"class= "Cn.sh.cares.framework.dao.hibernate.AuditInterceptor" /> </ Property>

This article transferred from: http://blog.csdn.net/pengchua/article/details/5013889

Interceptor Emptyinterceptor interface function in Hibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.