Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 4.0.0Final where is the SessionFactory.openSession(Interceptor interceptor)

Tags:

java

hibernate

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?

like image 880
pinkdawn Avatar asked Jul 11 '12 08:07

pinkdawn


People also ask

What are interceptors in hibernate?

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.

What is difference between getCurrentSession () and openSession () in hibernate?

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.

What is SessionFactory getCurrentSession ()?

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>


1 Answers

It's implemented using Builder pattern now:

Session session = sf.withOptions()
                    .interceptor(new AuditInterceptor())
                    .openSession(); 
like image 189
axtavt Avatar answered Oct 17 '22 17:10

axtavt