Hibernate: No CurrentSessionContext configured

ぃ、小莉子 提交于 2019-12-05 11:59:59

问题


I keep getting a :

HibernateException: No CurrentSessionContext configured!

in my code. The only information other searches that returned is that the culprit is:

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

in my hibernate.cfg.xml. I do have that in my hibernate.cfg.xml file, and I am running this in Tomcat 6. Does anyone know other possible causes?


回答1:


I believe you need to set your current_session_context_class to an actual class...I have my hibernate.cfg.xml configured for that as follows:

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

The above code is used for a mySql database but i don't think it matters what type the database is for the above property

Hope this helps




回答2:


Both of these work in Hibernate 4+:

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

In earlier version of Hibernate it used org.hibernate.context.ThreadLocalSessionContext (as shown in the other answers).




回答3:


In Hibernate 4.1.5 it is:

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



回答4:


I am using Hibernate 4 on tomcat 7 and the below property (the one mentioned in the question) worked for me.

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



回答5:


var config = new Configuration().Configure(); // Read config from hibernate.cfg.xml
config.CurrentSessionContext<WebSessionContext>(); // <-- No CurrentSessionContext fixing
return config.BuildSessionFactory();


来源:https://stackoverflow.com/questions/761480/hibernate-no-currentsessioncontext-configured

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!