Hi,
I want to record the last modification of a data record by using the event handling system. I wrote my own SaveOrUpdateEvent like this:
Code:
public class MySaveOrUpdateEventListener extends DefaultSaveOrUpdateEventListener {
public Serializable onSaveOrUpdate(SaveOrUpdateEvent event) {
if (event.getObject() instanceof IAuditedObject) {
IAuditedObject audObj = (IAuditedObject)event.getObject();
audObj.setLastModification(new Date());
}
return super.onSaveOrUpdate(event);
}
}
and registered this event like this in the hibernate.cfg.xml:
Code:
<listener type="save" class="MySaveOrUpdateEventListener"/>
<listener type="update" class="MySaveOrUpdateEventListener"/>
<listener type="save-update" class="MySaveOrUpdateEventListener"/>
As this woks fine when creating new records, the event handler is not called when updating an existing record like this:
Code:
MyObject myObj = HibernateUtil.getSession().load(MyObject.class, myIdId);
myObj.setAttr("some value");
Most likely the event handler is not called because I don't save the changed object explicitely on the session (session.saveOrUpdate(myObj)).
Which event handler will be called in this case? Do I have to extend more than one event handler or does there exist a event handler that will be called before every DB writing?
Thanks in advance,
Joern