版本:Hibernate_Core 3.3.2
在使用3.2的时候获取Session习惯使用sf.openSession();
但是现在开始流行getCurrentSession();了,原因很简单,openSession()是不管任何情况都重新开启一个Session,而getCurrentSession();相对的增加了一个判断,在有Session的情况下就会直接去调用,没有session的话才会创建,相对而言,好处显而易见了吧!
看看错误消息:没有CurrentSessionContext的配置!
呵呵.网上搜索了一下,原来
在集成Hibernate的环境下(例如Jboss),要在hibernate.cfg.xml中session-factory段加入:
- <property name="current_session_context_class">jta</property>
<property name="current_session_context_class">jta</property>
在不集成Hibernate的环境下(例如使用JDBC的独立应用程序),在hibernate.cfg.xml中session-factory段加入:
- <property name="current_session_context_class">thread</property>
<property name="current_session_context_class">thread</property>
现在再使用getCurrentSession()方法获取session.没有错误了