0

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 ?

Community
  • 1
  • 1
eXamScripts
  • 447
  • 1
  • 4
  • 11
  • Few questions - What error do you get when you use hibernate template? What is your spring library version? Post the exact code that fails to narrow down the issue. – Andy Dufresne Dec 22 '14 at 05:34
  • This is the error I get : Got the following exception: Exception in thread "Report Generator Thread" java.lang.NoSuchMethodError: org/hibernateSessionFactory.openSession() Lorg/hibernate/classic/Session; At the following line of code : List myList=templ.findByCriteria(subSelectCriteria, 0, 10); I opened up hibernate-core-4.3.4.Final.jar - and it has no org.hibernate.classic.Session this is present in 3.x version of hibernate while hibernate 4.x has org.hibernate.SessionFactory - this is what I think is breaking the usage of HibernateTemplate - am using Spring 4.0.4 – eXamScripts Dec 22 '14 at 05:41
  • Why aren't you simply using Spring to configure the session factory but are doing that yourself? Next to that the thread context should be the `SpringCurrentContext` fqn unless you are using JTA. – M. Deinum Dec 22 '14 at 08:42
  • ok thanks - I do not have more than 1 database - so I do not need JTA right ? - so the property should be set to : SpringCurrentContext - is that correct ? – eXamScripts Dec 22 '14 at 09:40

0 Answers0