Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 12. Interceptors and events
It is useful for the application to react to certain events that occur inside Hibernate. This allows for the implementation of generic functionality and the extension of Hibernate functionality.
12.1. Interceptors Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
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. One possible use for this is to track auditing information. For example, the following Interceptor automatically sets the createTimestamp when an Auditable is created and updates the lastUpdateTimestamp property when an Auditable is updated.
You can either implement
Interceptor directly or extend EmptyInterceptor.
There are two kinds of interceptors:
Session-scoped and SessionFactory-scoped.
A
Session-scoped interceptor is specified when a session is opened using one of the overloaded SessionFactory.openSession() methods accepting an Interceptor.
Session session = sf.openSession( new AuditInterceptor() );
Session session = sf.openSession( new AuditInterceptor() );
A
SessionFactory-scoped interceptor is registered with the Configuration object prior to building the SessionFactory. Unless a session is opened explicitly specifying the interceptor to use, the supplied interceptor will be applied to all sessions opened from that SessionFactory. SessionFactory-scoped interceptors must be thread safe. Ensure that you do not store session-specific states, since multiple sessions will use this interceptor potentially concurrently.
new Configuration().setInterceptor( new AuditInterceptor() );
new Configuration().setInterceptor( new AuditInterceptor() );