Exception in thread "main" org.hibernate.HibernateException:createQuery is not valid without active transactionat org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)at $Proxy0.create

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/28 22:59:28
Exceptioninthread"main"org.hibernate.HibernateException:createQueryisnotvalidwithoutactivetransactio

Exception in thread "main" org.hibernate.HibernateException:createQuery is not valid without active transactionat org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)at $Proxy0.create
Exception in thread "main" org.hibernate.HibernateException:createQuery is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
at $Proxy0.createQuery(Unknown Source)
at com.six.managerment.dao.impl.HibernateGenericDao.listAll(HibernateGenericDao.java:101)
at com.six.managerment.dao.impl.UserDaoImpl.main(UserDaoImpl.java:249)
我用下面的查询数据库
public T findById(Serializable id) {
return (T)getSession().get(persistClass,id);
}
用 getSessionFactory().getCurrentSession();获得session时就出上面的错误,如果用 getSessionFactory().openSession()获得session时 就不出错,有人说删除配置文件的 thread
或者修改 org.springframework.orm.hibernate3.SpringSessionContext就可以了,我试了不行,提示:org.hibernate.HibernateException:No Session found for current thread
获得Session
private Session getSession() {
return HibernateUtil.getSessionFactory().getCurrentSession();
}
配置文件如下:
thread
5
false
false
再麻烦你一下(只有最后这么点财富值了,不要嫌少啊)

Exception in thread "main" org.hibernate.HibernateException:createQuery is not valid without active transactionat org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)at $Proxy0.create
修改你的getSession方法
if(session == null || !session.isopen()){
return HibernateUtil.getSessionFactory().getCurrentSession();
}