Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hibernate SessionFactory openSession() vs getCurrentSession()

843859Oct 31 2007 — edited Nov 20 2014
Hi, I am using hibernate 3.2 and I try to implement the "session per request" paragdim with the org.hibernate.context.ThreadLocalSessionContext .

I don't understand as well the difference between openSession() and getCurrentSession() methods of the SessionFactory class.

I know that the session returned by openSession() is not attached to the current thread while the one return by getCurrentSession is. But the 2 session seems to be in different states. For example, the following code works fine :
org.hibernate.Session sessionHibernate = com.apis.b7afn.wda.mapping.util.HibernateUtil.getSessionFactory().openSession(); 
sessionHibernate.createQuery("from B7AFNWDATTypologie where valeur = 'typeAchat'").list(); 
out.println("ok"); 
and this one don't work :
org.hibernate.Session sessionHibernate = com.apis.b7afn.wda.mapping.util.HibernateUtil.getSessionFactory().getCurrentSession(); 
sessionHibernate.createQuery("from B7AFNWDATTypologie where valeur = 'typeAchat'").list(); 
out.println("ok"); 
 
In the second piece of code I have to open a transaction to run my query. This is not necessary in the first one.

How can I execute a query using the second way without opening a transaction?

Thanks for reply.

My apologies for my english.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 3 2007
Added on Oct 31 2007
3 comments
1,096 views