错误整理(五)No CurrentSessionContext configured!

来源:互联网 发布:mac 无法启动安全模式 编辑:程序博客网 时间:2024/05/24 06:22

一、 错误类型: 使用 Hibernate 的 getCurrentSession() 时会出现如下错误:

这里写图片描述


二、 错误原因: 这是由于使用 getCurrentSession() 函数造成的,意思是没有 CurrentSessionContext 的配置。


三、 解决方法: 有两种解决方法

  • 解决方法 1: 使用 openSession() 替换 getCurrentSession()

  • 解决方法 2: 修改 hibernate.cfg.xml 配置文件

(1)如果使用的是本地 JDBC 事物,则在<session-factory>标签中加入

<property name="current_session_context_class">thread</property>

(2)如果使用的是全局事物,则在<session-factory>标签中加入

<property name="current_session_context_class">jta</property>

注意: hibernate.cfg.xml 文件读取时是按顺序读取的,“property”标签应该在 “mapping”标签之上。

0 0
原创粉丝点击