本文主要是介绍错误:org.hibernate.HibernateException: No CurrentSessionContext configured!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
版本:Hibernate_Core 3.3.2在使用3.2的时候获取Session习惯使用sf.openSession();
但是现在开始流行getCurrentSession();了,原因很简单,openSession()是不管任何情况都重新开启一个Session,而getCurrentSession();相对的增加了一个判断,在有Session的情况下就会直接去调用,没有session的话才会创建,相对而言,好处显而易见了吧!
我碰见的错误消息
- org.hibernate.HibernateException: No CurrentSessionContext configured!
- at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:620)
- at com.hibernate.test.HibernateTest.testSave01(HibernateTest.java:23)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- at java.lang.reflect.Method.invoke(Method.java:597)
- at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
- at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
- at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
- at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
- at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
- at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
- at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
- at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
- at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
- at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
- at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
- at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
- at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
- at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
看看错误消息:没有CurrentSessionContext的配置!
呵呵.网上搜索了一下,原来
在集成Hibernate的环境下(例如Jboss),要在hibernate.cfg.xml中session-factory段加入:
- <property name="current_session_context_class">jta</property>
在不集成Hibernate的环境下(例如使用JDBC的独立应用程序),在hibernate.cfg.xml中session-factory段加入:
- <property name="current_session_context_class">thread</property>
现在再使用getCurrentSession()方法获取session.没有错误了, 马上过年了,大家写码愉快!
这篇关于错误:org.hibernate.HibernateException: No CurrentSessionContext configured!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!