posted 10 years ago
Once the object has been changed, it must be saved back to the database. This process continues until the next time the object is needed, and it will be loaded from the persistent store. Interceptor Interface provides methods which can be called at different lifecycle stages to perform some required tasks. These methods are 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 an you do not have to explicitly call them. When the flush() method is called on an entity, the existing state in the database is checked with the entity's state and if there are any changes, it is treated as dirty, which means that an update operation is required at the database end.
To call an interceptor, you need to set it while opening the session or session factory:
HibernateUtil.getSessionFactory().openSession(<interceptor object>)
new Configuration().setInterceptor( <interceptor object> );
It is useful if you are debugging some problem with the state and/or need persistence operation occurrence for audit/logging/security related events.