We were using Spring HibernateTemplate with Hibernate
The HibernateTemplate was created by reading the Hibernate.cfg file
SessionFactory sf = new AnnotationConfiguration().configure
("/com/examscripts/exam.hibernate.cfg.xml").buildSessionFactory();
HibernateTemplate ht = new HibernateTemplate(sf);
The Hibernate.cfg contains a Datasource in weblogic which is pointing to mySql
<session-factory>
<property name="connection.datasource">MYSQLDS</property>
<property name="default_schema">examSch</property>
</session-factory>
After moving to Hibernate 4.x I found that I cannot use HibernateTemplate
So I used the SessionFactory directly - but it started giving an error :
org.hibernate.HibernateException: No CurrentSessionContext configured!
I am getting this exception when I am doing this :
Session session = sf.getCurrentSession();
I am puzzled - so in Hibernate 3.x with spring hibernate template I did not get such an exception -
so why am I getting it in 4.x
Was HibernateTemplate taking care of this ?
I also read that now in my cfg.xml i will need to provide :
<property key="hibernate.current_session_context_class">jta</property>
Should it be jta or thread ?
I am assuming jta since I am using datasource from weblogic - is that correct ?
NOTE - tried reading : http://relation.to/2037.lace
Spring Transactions and hibernate.current_session_context_class
What is contextual sessions in hibernate?
using current_session_context_class property hibernate 3 hibernate 4
BUT I am a noob - it made no sense to me :(
EDIT / UPDATE 1
I made the following entry in my hibernate.cfg file:
Changed it to:
<property name="hibernate.current_session_context_class">thread</property>
Exception:
org.hibernate.HibernateException: createQuery is not valid without active transaction
at the line:
Session session = getSessionFactoryCustom().getSessionFactoryTest().getCurrentSession();
List<Person> personList = session.createQuery("from Person").list(); // exception is here
Then I tried changing it to :
<property name="hibernate.current_session_context_class">SpringCurrentContext</property>
Now the exception is :
org.hibernate.HibernateException: No CurrentSessionContext configured!
at line:
Session session = SessionFactory.getCurrentSession();
Then I tried changing it to :
<property name="hibernate.current_session_context_class">jta</property>
Now the exception is :
org.hibernate.HibernateException: No TransactionManagerLookup specified
at org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:85)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at com.mkyong.common.dao.TestDaoImpl.list(TestDaoImpl.java:56)
TestDaoImpl at line 56:
Session session = SessionFactory.getCurrentSession();
So now which TransactionManagerLookup should I specify ?
Does anyone have a working example of their hibernate.cfg with the necessary configurations that will work with weblogic datasource ?