I am trying to implement (can you guess?) an auditing function. When I save or update any object I would like to set the userId of the person who made the change and the date. All tables will have these fields.
So far I have this in my Spring applicationContext-dao.xml:
Code:
<bean id="sessionFactory" ...>
<property name="eventListeners">
<map>
<entry key="save-update"><ref local="saveOrUpdateListener" /></entry>
</map>
</property>
</bean>
<bean id="saveOrUpdateListener" class="com.myco.myapp.dao.SaveOrUpdateListener" />
And this is my class:
Code:
public class SaveOrUpdateListener extends DefaultSaveOrUpdateEventListener
{
public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException
{
System.out.println("Hello from SaveOrUpdateListener!!!");
super.onSaveOrUpdate(event);
}
}
The class is not being called when I save or update. I am not sure about the name of the listener in applicactionContext-dao.xml. All the examples on the web use the DefaultLoadEvenListener and use "load" as the name. I've tried using "save", "save-or-update", "save-update", nothing works.
Assuming I get this working I am not too sure exactly how to update the two columns, userId and dateMod. Using an Interceptor it was quite easy. You just cycle through the property names and set the ones you want. But my Interceptor didn't work for updates, only inserts (saves) so I was told to use EventListener instead.
Has anyone ever successfully implemented this?
Many thanks,
Bob