在使用 SessionFactory 的 getCurrentSession 方法时遇到如下异常 “No CurrentSessionContext configured 

 

原因是:

在hibernate.cfg.xml文件中缺少如下属性设置:hibernate.current_session_context_class

 

修正方法如下:

如果是Web项目,则在hibernate.cfg.xml中加入这句话:

1 <property name="hibernate.current_session_context_class">jta</property>

 

如果是Java Application,则这样设置:

1 <property name="hibernate.current_session_context_class">thread</property>

 

 

P.S:如果hibernate.cfg.xml这个配置文件是从其他项目或者Hibernate的文档上 Copy-修改,这个属性 hibernate.current_session_context_class 是有的。

笔者之所以遇到这样的问题,是使用了Hibernate的反向工程,自动生成的配置文件。

 

相关文章:

  • 2022-12-23
  • 2021-10-06
  • 2021-08-27
  • 2022-12-23
  • 2021-07-16
  • 2022-02-12
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2021-08-26
  • 2021-05-21
  • 2022-12-23
  • 2021-10-02
  • 2021-09-12
相关资源
相似解决方案