I try some code from hibernate 4.0 interceptors, which gives this code for use session-level interceptors:
Session session = sf.openSession( new AuditInterceptor() );
however, I check both the hibernate-core 4.0 source code and onlie hibernate 4.0 java-doc, the class SessionFactory
does not have method openSession(Interceptor interceptor)
, but hibernate 3.6 java-doc do have this method.
anyone knows where is the method move to? if deprecated, why the document still keeps it in tutorial document? and how should I use session-level interceptor in 4.0?
The Hibernate Interceptor is an interface that allows us to react to certain events within Hibernate. These interceptors are registered as callbacks and provide communication links between Hibernate's session and application.
openSession() always opens a new session that you have to close once you are done with the operations. SessionFactory. getCurrentSession() returns a session bound to a context - you don't need to close this.
Hibernate SessionFactory getCurrentSession() method returns the session bound to the context. But for this to work, we need to configure it in hibernate configuration file like below. <property name="hibernate.current_session_context_class">thread</property>
It's implemented using Builder pattern now:
Session session = sf.withOptions()
.interceptor(new AuditInterceptor())
.openSession();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With