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:
从方法主体可以明显看出,此属性的值应为:
jta
for getting JTASessionContextjta
获取JTASessionContextmanaged
for ManagedSessionContext 为ManagedSessionContextmanaged
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