Friday, November 20, 2009

Hiberante Event Listeners

Hibernate also provides a event System besides the Interceptor Model.The Event System can be used in place of Interceptor or besides the interceptors.The Hibernate system provides many methods that work with the operations that we perfom on a pojo object.there are events like save,saveOrUpdate and many other .

These events are linked with the session.what ever the event a session generates is handled by the appropriate event handler linked with the current

sessionfactory.these event listeners are said to be singleton.these events listeners are shared among many request.no values are to be hold in the instance variables of the event listeners.A custom listener implements the appropriate interface for the event it wants to process and/or extend one of the convenience base classes (or even the default event listeners used by Hibernate out-of-the-box as these are declared non-final for this purpose).


Lets see how we can write a simple event listener while storing an object , we will be seeing a event listener system for the save-update event.


There are 2 ways to perform the save-update event using event listener either implement a interface called “SaveOrUpdateEventListener” or either extends a class “DefaultSaveOrUpdateEventListener”.the method obtained when extending the class or by implementing the interface leads us to a method ,

Public void onSaveOrUpdate(SaveUpdateEvent event){ }


When we have written a event listener class, we need to attach it the current SessionFactory such that when ever a event of that is throw by the session, the listener class linked to the current session factory gets executed.


Now lets see how we write a event listener by extending the class ,


@SuppressWarnings("deprecated")
public class DefaultSaveOrUpdateListener extends DefaultSaveOrUpdateEventListener {


public void onSaveOrUpdate(SaveOrUpdateEvent object) {


MyData data=(MyData) object.getObject();
data.setFirstName(data.getFirstName().trim()+”Trimmed”);
super.onSaveOrUpdate(object);


}
}


Or else we can do the same thing by implementing the interface ,


@SuppressWarnings("deprecated")
public class DefaultListenerClass implements SaveOrUpdateEventListener {


public void onSaveOrUpdate(SaveOrUpdateEvent arg0)
throws HibernateException {


}
}
When ever we say saveOrUpdate , the event for the save-Update which we registered with the sessionFactory gets called and processed.this is the code for saving my object,


obtainedFactory=HibernateSessionFactory.getSessionFactory();
obtainedSession=obtainedFactory.getCurrentSession();
obtainedSession.beginTransaction();
obtainedSession.saveOrUpdate(new MyData("Very Much","MySelf"));
obtainedSession.connection().commit();
obtainedSession.close();




as We have written in the above onSaveOrUpdate() method , the first Name we send will be appended with “new Much “ value and is saved in the DataBase.but how are we going to register the eventListener to the SessionFactory ,


configuration.setListener("save-update", new DefaultListenerClass());


I just linked the Event Listener to save-Update event and attached it to the SessionFactory . we can do the same by adding in the Hiberante.cfg.xml file also.

This gives the same effect. We can write listeners for various events like
Save,update,delete , flush e.t.c.

Read More