版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kiss_xiaojie/article/details/77644619
[Error]: createQuery is not valid without active transaction
- 在整合
Struts2
Spring
以及Hibernate5
时,最终出现上述异常,还有一下现象:
利用 hibernate 自动创建表时,log 中出现了执行的 sql 语句而数据库中却没有创建出来。
- 最终发现是因为 Hibernate 的 核心配置 中添加了如下配置:
<property name="hibernate.current_session_context_class">thread</property>
也就是把
session
绑定到 hibernate 线程。然而这时,我们已经把sessionFatory
的创建交给了Spring
所以此时这样绑定是不对的。可以修改为:
<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</property>
- 最后感谢