Hibernate Interceptor
In this tutorial you will learn about Hibernate Interceptor.
Interceptors hooks the different types of events or actions before or after the core functional logic, these events can be a set of pre-requisite or post- requisite operations. Mostly Framework calls callback methods to response a configured and registered events or actions. In Hibernate an interface Interceptor provides a pluggable feature to this framework to callbacks from the session to the application that permit the application to look over and/or manipulate the object before its loading, deleting, updating, saving. An interface Interceptor can be implemented directly or a class EmptyInterceptor can be extended.
Types of Interceptor :
There are two types of Interceptor based on their scope :
- Session-scoped Interceptor : Such interceptor is determined when SessionFactory.openSession() method is called that accepts an interceptor. Entire persistent objects, will be affected associated within that particular session.
- SessionFactory-scoped Interceptor : This interceptor is specified with the Configuration object before the creation of SessionFactory. This interceptor is used as a global interceptor and is applicable to all the sessions that are opened from same SessionFactory.
for example :
MyInterceptor.java is an interceptor
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 MyInterceptor extends EmptyInterceptor { private int upd; private int cre; private int ld; public void onDelete(Object obj, Serializable id, Object[] states, String[] propNames, Type[] types) { } public boolean onFlush(Object obj, Serializable id, Object[] curState, Object[] preState, String[] propNames, Type[] types) { if ( entity instanceof Auditable) { upd++; for ( int i=0; i < propNames.length; i++ ) { if ( "lastUpdateTimestamp".equals( propNames[i] ) ) { curState[i] = new Date(); return true; } } } return false; } public boolean onLoad(Object obj, Serializable id, Object[] state, String[] propNames, Type[] types) { if ( entity instanceof Auditable ) { ld++; } return false; } public boolean onSave(Object obj, Serializable id, Object[] state, String[] propNames, Type[] types) { if ( entity instanceof Auditable ) { cre++; for ( int i=0; i<propNames.length; i++ ) { if ( "createTimestamp".equals( propNames[i] ) ) { state[i] = new Date(); return true; } } } return false; } public void afterTransactionCompletion(Transaction tx) { if ( tx.wasCommitted() ) { System.out.println("Create: " + cre + ", Update: " + upd, "Load: " + ld); } upd=0; cre=0; ld=0; } }
An interceptor MyInterceptor can be used as follows :
In Session-scoped Interceptor
Configuration cfg = new Configuration(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry); MyInterceptor mIn = new MyInterceptor(); Session session = sessionFactory.openSession( new mIn );
In SessionFactory-scoped Interceptor
Configuration cfg = new Configuration(); cfg.setInterceptor(new MyInterceptor()); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);