Hibernate requires a lot of configurations and sometimes you get weird exception and there is no clue that it’s because of some missing configuration.

Hibernate需要大量的配置,有时您会得到奇怪的异常,并且没有任何线索是由于缺少某些配置。

org.hibernate.HibernateException:未配置CurrentSessionContext (org.hibernate.HibernateException: No CurrentSessionContext configured)

I was working on a simple hibernate web application and getting below exception.

我正在开发一个简单的HibernateWeb应用程序,并且遇到异常。

org.hibernate.HibernateException: No CurrentSessionContext configured!org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1012)com.journaldev.servlet.hibernate.GetEmployeeByID.doGet(GetEmployeeByID.java:31)javax.servlet.http.HttpServlet.service(HttpServlet.java:621)javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

When I looked at the source code, the statement that was throwing the exception was;

当我查看源代码时,引发异常的语句是:

Session session = sessionFactory.getCurrentSession();

My Hibernate configuration was like this:

我的Hibernate配置是这样的:

hibernate.cfg.xml

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""https://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.connection.datasource">java:comp/env/jdbc/MyLocalDB</property><!-- Mapping with model class containing annotations --><mapping class="com.journaldev.servlet.hibernate.model.Employee"/></session-factory>
</hibernate-configuration>

修复org.hibernate.HibernateException:未配置CurrentSessionContext (Fix for org.hibernate.HibernateException: No CurrentSessionContext configured)

Everything looked fine to me and the exception didn’t clearly says what is missing.

在我看来,一切都很好,但异常情况并未明确说明缺少的内容。

After going through the SessionFactoryImpl, I found that we also need to configure the current session context class to get the current session.

经过SessionFactoryImpl ,我发现我们还需要配置当前会话上下文类以获取当前会话。

This is done in method buildCurrentSessionContext() and it looks for property hibernate.current_session_context_class.

这是在方法buildCurrentSessionContext() ,它查找属性hibernate.current_session_context_class

From the method body, it became clear that the value of this property should be:

从方法主体可以明显看出,此属性的值应为:

  1. jta for getting JTASessionContextjta获取JTASessionContext
  2. managed for ManagedSessionContext为ManagedSessionContext managed
  3. thread for ThreadLocalSessionContextThreadLocalSessionContext的thread

The method also works if we provide above class name. So when I added this property in the hibernate configuration file, program started working fine.

如果我们提供上述类名,则该方法也适用。 因此,当我在Hibernate配置文件中添加此属性时,程序开始正常运行。

Hibernate CurrentSessionContext类的配置示例 (Hibernate CurrentSessionContext class configuration example)

Sample ways to define current_session_context_class property are:

定义current_session_context_class属性的示例方法是:

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

Use below configuration if your hibernate configuration file is having xml format.

如果您的Hibernate配置文件具有xml格式,请使用以下配置。

<property name="hibernate.current_session_context_class">
org.hibernate.context.internal.ThreadLocalSessionContext
</property>

Thats’s all for fixing org.hibernate.HibernateException: No CurrentSessionContext configured. I hope it will save you some time.

修复org.hibernate.HibernateException就是这样:没有配置CurrentSessionContext。 我希望它可以节省您一些时间。

翻译自: https://www.journaldev.com/2903/org-hibernate-hibernateexception-no-currentsessioncontext-configured

[已解决] org.hibernate.HibernateException:未配置CurrentSessionContext相关推荐

  1. [已解决] org.hibernate.HibernateException:没有活动事务,get无效

    Recently I was developing a simple hibernate application with version 4.3.5.Final and everything loo ...

  2. [已解决] org.hibernate.AnnotationException:未为实体类指定标识符

    Recently I was working on a hibernate project and I have added few entity beans, when executed I got ...

  3. 解决 org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does no

    1.getCurrentSession()是必须提交事务的.故使用getSessionFactory().getCurrentSession()时,这个方法一定是要配置声明式事务管理. 2.openS ...

  4. 已解决:Java环境变量配置后不生效

    一.问题 从jdk8升级到jdk11,配置JAVA_HOME后,不生效. (备注:jdk8是安装版,jdk11是解压版.) 二.解决办法 在环境变量Path中,删除下面的配置: C:\Program ...

  5. Linux iNode 双网卡,已解决: Zynq 7000 双网卡配置-内核DTS该如何配置 - Community Forums...

    问题:ETH0是通的,ETH1找不到PHY(连接到GMII2RGMII转换器时,找不到该设备),dts和vivado该如何配置? 系统:Zynq 7Z015 Vivado:2018.3 内核:4.6 ...

  6. 已解决‘pyhton‘不是内部或外部命令,也不是可运行的程序或批处理文件。

    已解决(安装python解释器配置环境变量后,在cmd内运行报错问题)'pyhton'不是内部或外部命令,也不是可运行的程序或批处理文件. 文章目录 报错代码 报错原因 解决方法 千人全栈VIP答疑群 ...

  7. [已解决]Hibernate程序未终止

    Recently I was writing a small hibernate program and noticed that the program was not terminating ev ...

  8. 未找到插件 ‘org.springframework.bootspring-boot-maven-plugin‘(已解决 )

    未找到插件 'org.springframework.boot:spring-boot-maven-plugin:'(已解决 ) 加上一个版本号即可 <plugin><groupId ...

  9. 微信支付的服务器配置url超时,微信H5支付商家存在未配置的参数,请联系商家解决的...

    前言 考虑到网络上微信的h5支付都是讲解原生app的,ionic3的基本没有,我自己总结下. 大致流程 1.用户在商户侧完成下单,使用微信支付进行支付 2.由商户后台向微信支付发起下单请求(调用统一下 ...

最新文章

  1. mysql sql variant_SQL 数据类型
  2. 让你编程能力秃飞猛进的好习惯
  3. OpenCV中基本数据结构(4)_Rect
  4. Flink 1.9 : Wordcount报错:ClassNotFoundException: yarn.exceptions.YarnException
  5. 优雅的实现对外接口,要注意哪些问题?
  6. 在C#中使用自定义消息
  7. hihoCoder 1388(fft)
  8. android fastboot 工具,fastboot工具中文帮助文档
  9. day53_电力系统_ztree动态生成权限控制
  10. C4D怎么将模型保存为预设?如何自定义预置库,如何修改别人的预置库?
  11. HSB”、lab、CMYK、RGB有什么区别
  12. 关于交换的知识点(一)
  13. Swift-字符串和字符
  14. 软件测试——课程感想
  15. 用Python写一个简单的24点计算器
  16. css情景动画,css3中的动画属性animation应用场景及编写代码教程
  17. EF中的EntityState几个状态的说明
  18. 在小程序中安装有赞的vant weapp
  19. 如何取消共享计算机管理员权限设置密码,Win7旗舰版系统共享文件夹设置密码如何设置...
  20. CAD对块的文字操作

热门文章

  1. 数据库设计经验浅谈(3,4,5)转载
  2. 深入ASP.NET 2.0的提供者模型
  3. [转载] Python基础:什么是字符串?字符串是用来做什么的?
  4. [转载] 消息中间件学习总结(8)——RocketMQ之RocketMQ捐赠给Apache那些鲜为人知的故事
  5. docker入门与部署微服务--学习笔记
  6. 带新手玩转MVC——不讲道理就是干(下)
  7. Linux管理传世经典:Linux 系统管理技术手册(第二版) 中文高清版下载
  8. webuploader
  9. EntityFramework6.X 之 Operation
  10. libstdc和glibc的一些共享库问题