When I wanted to use hibernate event listeners or interceptors to modify an entity before it is saved and after it is loaded from the DB, I did not find a satisfying list of available events! Fortunately I found this post (in German) which simply listed all hibernate mappings from the code:
http://www.nautsch.net/2008/05/16/hibernate-eventlisteners-in-spring-konfigurieren/
I think – as I too didn’t find a better documentation about it – this is a great idea.
And it proves once more: Code is the best documentation.
static {
eventInterfaceFromType = new HashMap();
eventInterfaceFromType.put("auto-flush", AutoFlushEventListener.class);
eventInterfaceFromType.put("merge", MergeEventListener.class);
eventInterfaceFromType.put("create", PersistEventListener.class);
eventInterfaceFromType.put("create-onflush", PersistEventListener.class);
eventInterfaceFromType.put("delete", DeleteEventListener.class);
eventInterfaceFromType.put("dirty-check", DirtyCheckEventListener.class);
eventInterfaceFromType.put("evict", EvictEventListener.class);
eventInterfaceFromType.put("flush", FlushEventListener.class);
eventInterfaceFromType.put("flush-entity", FlushEntityEventListener.class);
eventInterfaceFromType.put("load", LoadEventListener.class);
eventInterfaceFromType.put("load-collection", InitializeCollectionEventListener.class);
eventInterfaceFromType.put("lock", LockEventListener.class);
eventInterfaceFromType.put("refresh", RefreshEventListener.class);
eventInterfaceFromType.put("replicate", ReplicateEventListener.class);
eventInterfaceFromType.put("save-update", SaveOrUpdateEventListener.class);
eventInterfaceFromType.put("save", SaveOrUpdateEventListener.class);
eventInterfaceFromType.put("update", SaveOrUpdateEventListener.class);
eventInterfaceFromType.put("pre-load", PreLoadEventListener.class);
eventInterfaceFromType.put("pre-update", PreUpdateEventListener.class);
eventInterfaceFromType.put("pre-delete", PreDeleteEventListener.class);
eventInterfaceFromType.put("pre-insert", PreInsertEventListener.class);
eventInterfaceFromType.put("pre-collection-recreate", PreCollectionRecreateEventListener.class);
eventInterfaceFromType.put("pre-collection-remove", PreCollectionRemoveEventListener.class);
eventInterfaceFromType.put("pre-collection-update", PreCollectionUpdateEventListener.class);
eventInterfaceFromType.put("post-load", PostLoadEventListener.class);
eventInterfaceFromType.put("post-update", PostUpdateEventListener.class);
eventInterfaceFromType.put("post-delete", PostDeleteEventListener.class);
eventInterfaceFromType.put("post-insert", PostInsertEventListener.class);
eventInterfaceFromType.put("post-commit-update", PostUpdateEventListener.class);
eventInterfaceFromType.put("post-commit-delete", PostDeleteEventListener.class);
eventInterfaceFromType.put("post-commit-insert", PostInsertEventListener.class);
eventInterfaceFromType.put("post-collection-recreate", PostCollectionRecreateEventListener.class);
eventInterfaceFromType.put("post-collection-remove", PostCollectionRemoveEventListener.class);
eventInterfaceFromType.put("post-collection-update", PostCollectionUpdateEventListener.class);
eventInterfaceFromType = Collections.unmodifiableMap( eventInterfaceFromType );
}
And this is how you can use this information:
In Spring, you can configure an event listener for a session factory:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
[...]
<property name="eventListeners">
<map>
<entry key="save-update">
<ref local="yourSaveOrUpdateListener" />
</entry>
<entry key="save">
<ref local="yourSaveOrUpdateListener" />
</entry>
<entry key="update">
<ref local="yourSaveOrUpdateListener" />
</entry>
<entry key="post-load">
<ref local="yourPostLoadListener" />
</entry>
[...]
</map>
</property>
[...]
</bean>
You can use the key in Hibernate’s map above to know which events are available and how to name the key in the spring configuration. The class name above tells you which interface to implement for the given listener.