We will extend EmptyInterceptor in our example where Interceptor's method will be called automatically when Employee object is created and updated. You can implement more methods as per your requirements.
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; publicclassMyInterceptorextendsEmptyInterceptor{ privateint updates; privateint creates; privateint loads; publicvoid onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types){ // do nothing } // This method is called when Employee object gets updated. publicboolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types){ if( entity instanceofEmployee){ System.out.println("Update Operation"); returntrue; } returnfalse; } publicboolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types){ // do nothing returntrue; } // This method is called when Employee object gets created. publicboolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types){ if( entity instanceofEmployee){ System.out.println("Create Operation"); returntrue; } returnfalse; } //called before commit into database publicvoid preFlush(Iterator iterator){ System.out.println("preFlush"); } //called after committed into database publicvoid postFlush(Iterator iterator){ System.out.println("postFlush"); } } |