[已解决] org.hibernate.HibernateException:未配置CurrentSessionContext

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 JTASessionContext

    jta获取JTASessionContext
  2. managed for ManagedSessionContext

    为ManagedSessionContext managed
  3. thread for ThreadLocalSessionContext

    ThreadLocalSessionContext的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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值