一、 错误类型: 使用 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”标签之上。