I have a module which uses Hibernate as an ORM solution with EHCache as second level cache. I have another seperate module which inserts and updates the database. What I need is to have the ability to trigger an event when a row is inserted or updated. Let's say I have a Customers table and it is mapped to a Customer entity. I want some p开发者_如何学编程rocedure to notify me that a new Customer has been added. Regarding the second seperate module it uses Hibernate also but at least for the time being they are not connected (I'm pointing this out as if someone thinks that I must share the Hibernate session (or something of the sort) between them then this is something I will consider). Please note that I have limited experience with Hibernate. Thanks in advance
A Hibernate interceptor is likely what you need
http://docs.jboss.org/hibernate/stable/core/reference/en/html/events.html
The Interceptor interface provides callbacks from the session to the application, allowing the application to inspect and/or manipulate properties of a persistent object before it is saved, updated, deleted or loaded.
There's a simple example of how to write one and configure it.
There's also the Event system, which is a lower-level hook into SQL-level operations, but that's probably a bit more complex than you need.
精彩评论