How can I configure hibernate event listeners "post-insert", "post-delete", "post-load", "post-update" in java configuration? Either thru annotations or using Spring IOC? Also, is there a way to configure for a specific entity rather than in the method checking instanceof?
1 Answer
You can use the JPA annotations @PostLoad, @PostPersist, @PostRemove, @PostUpdate, @PrePersist, @PreRemove, @PreUpdate in void methods without parameters inside your entity class. They will be called when the event is performed for a specific entity.
http://download.oracle.com/javaee/5/api/index.html?javax/persistence/package-summary.html
4 Comments
joshjdevl
thx. Any ideas how to use post load for the following code? opensource.atlassian.com/projects/hibernate/browse/HHH-1395 final NestedSetNode node = (NestedSetNode) event.getEntity(); node.setPreviousParent(node.getParent()); new InsertNestedSetOperation( (NestedSetNode<?>) event.getEntity()).execute(event.getSession());
jpkroehling
You mean, other than what is discussed in that JIRA? No, sorry.
joshjdevl
Created separate question for listener that requires session stackoverflow.com/questions/4683820/…
Mike Baranczak
Warning: this only works when you access Hibernate through JPA. If you use the Hibernate Session object directly, those annotations are ignored.