After I found the solution above, I found more problem.
This is something to do with a background thread. I try to solve this problem by doing this:
Code:
try {
session = sessionFactory().openSession();
tx = session.beginTransaction();
//Read some object here.
//Update the object.
tx.commit();
} catch (Exception e) {
if (tx != null) tx.rollback();
} finally {
if (session != null) session.close();
}
The problem is that the read object use hibernateTemplate.find to locate the object, and a similar exception is shown. So, I change the code to:
Code:
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(SomeClass.class);
criteria.add(Expression.eq("someField", fieldValue));
List l = criteria.list();
if (l.size() > 0) {
return (SomeClass) l.get(0);
}
This code, give me the following error:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
How do we deal with background thread here?
Thanks.
Code: