Ok, so here is my solution which works - the unit tests pass anyway ;)
I've made a listener...
Code:
public class TraxxViewListener implements PostLoadEventListener,
SaveOrUpdateEventListener {
private static DefaultPostLoadEventListener postLoad = new DefaultPostLoadEventListener();
private static DefaultSaveOrUpdateEventListener saveUpdate = new DefaultSaveOrUpdateEventListener();
public void onPostLoad(final PostLoadEvent event) {
if (event.getEntity() instanceof TraxxView) {
final TraxxView view = (TraxxView) event.getEntity();
view.deserialiseViewConfig();
}
postLoad.onPostLoad(event);
}
public void onSaveOrUpdate(final SaveOrUpdateEvent event)
throws HibernateException {
if (event.getObject() instanceof TraxxView) {
System.out.println("Serialising view");
final TraxxView view = (TraxxView) event.getObject();
view.serialiseViewConfig();
event.setEntity(view);
}
saveUpdate.onSaveOrUpdate(event);
}
}
...and put it in the hibernate config like this...
Code:
<listener class="com.fugro.traxx.entities.TraxxViewListener" type="post-load"/>
<listener class="com.fugro.traxx.entities.TraxxViewListener" type="save-update"/>
The entity class is the same. So this works, but it seems messy to me. Apart from the fact that the listener hears ALL post load/save events (and I have to check the type of object which is being persisted) when I only want it to listen for persist events for TraxxView, I also have to delegate to the default listener implementations just to keep everything else working happily. This doesn't look good for having to create listeners for other entities - it seems code for all entities would be in this one listener class (i.e. one listener per event - I just combined the two here because I wanted the code in one place).
So, is this right and just a bit messy because of the way Hibernate does things? Or am I just barking up the wrong tree?