Hibernate Interceptor

In this tutorial you will learn about Hibernate Interceptor.


 

Ads

Tutorials   
How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code
Ads

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);

Advertisements

Ads
Share on Google+Share on Google+

Hibernate Interceptor

Posted on: March 15, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Hibernate Interceptor  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
SKP
March 30, 2012
HI

Good...
Khalil
April 19, 2012
Thanks

This was very helpful, thank you

Ads

 

Ads