Tuesday, June 23, 2009

Hibernate Event Listeners with Spring

You can configure your Hibernate event listeners via your spring-context.xml.
When you declare your Bean from type org.springframework.orm.hibernate3.LocalSessionFactoryBean you can initilize the property "eventListeners". This attribute is from type Map. The key defines the type of the event listeners (e.g. flush, load, delete, etc.). The value defines the corresponding class which implements the EventListener interface or extend the DefaultEventListener implementation class.


Sample of your context.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
...
<property name="eventListeners">
<map>
<entry key="flush">
<bean class="org.j4fry.hibernate.eventlisteners.FlushEventListener" />
</entry>
</map>
</property>
...
</bean>
Sample of a FlushEventListener

package org.j4fry.hibernate.eventlisteners;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.event.FlushEvent;
import org.hibernate.event.def.DefaultFlushEventListener;

public class FlushEventListener extends DefaultFlushEventListener {
public void onFlush(FlushEvent fe) throws HibernateException {
super.onFlush(fe);
}
}
List of keys and the corresponding Interfaces and implementation-classes
Map-KeyInterfaceDefault implementation class
auto-flushAutoFlushEventListener
deleteDeleteEventListener
dirty-checkDirtyCheckEventListener
evictEvictEventListener
flush-entityFlushEntityEventListener
flushFlushEventListener
load-collectionInitializeCollectionEventListener
loadLoadEventListener
lockLockEventListener
mergeMergeEventListener
create-onflushPersistEventListener
post-deletePostDeleteEventListener
post-insertPostInsertEventListener
post-loadPostLoadEventListener
post-updatePostUpdateEventListener
pre-deletePreDeleteEventListener
pre-insertPreInsertEventListener
pre-loadPreLoadEventListener
pre-updatePreUpdateEventListener
refreshRefreshEventListener
replicateReplicateEventListener
save-updateSaveOrUpdateEventListener

No comments:

Post a Comment