rg.hibernate.HibernateException:没有活动事务,get无效
时间:2020-02-23 14:44:23 来源:igfitidea点击:
最近我正在用4.3.5.Final版本开发一个简单的休眠应用程序。
org.hibernate.HibernateException: get is not valid without active transaction org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) com.sun.proxy.$Proxy26.get(Unknown Source)
org.hibernate.HibernateException:没有活动的事务,get无效
引发异常的休眠代码段是:
Session session = sessionFactory.getCurrentSession(); //below line throws exception Employee emp = (Employee) session.get(Employee.class, empId);
经过一些调试后,我发现我们需要启动事务来解决此问题。
所以我将上面的代码片段更改为;
Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); Employee emp = (Employee) session.get(Employee.class, empId); tx.commit();
问题消失了,一切工作正常。