Hibernate(三): org.hibernate.HibernateException: No CurrentSessionContext configured!

Hibernate版本5.2.9

获取Session的方式是sessionFactory.getCurrentSession();

比较老一些的版本使用的是sessionFactory.openSession()是不管任何情况都重新开启一个Session。

从两者却别来说getCurrentSession();相对的增加了一个判断,在有Session的情况下就会直接去调用,没有session的话才会创建。比openSession()要好一点。

但是,目前却遇到了问题:

四月 15, 2017 1:38:33 下午 org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@4ebea12c'
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:454)
    at com.dx.hibernate5.test.HelloWord.main(HelloWord.java:43)

网上搜索了一下,找到了解决问题的方案:

1、如果是Hibernate集成了JBoss的情况下,需要在*.cfg.xml配置文件中新增配置:

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

2、如果是不继承的话,需要在*.cfg.xml配置文件中新增配置:

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

经调试,问题解决了。

原文地址:https://www.cnblogs.com/yy3b2007com/p/6714112.html