今天,在做关于hibernate的练习,结果一切都是与题目无异,但却报出
在集成Hibernate的环境下(例如Jboss),要在hibernate.cfg.xml中session-factory段加入:
在不集成Hibernate的环境下(例如使用JDBC的独立应用程序)(我的情况就是这个),在hibernate.cfg.xml中session-factory段加入:
现在再使用getCurrentSession()方法获取session.没有错误了,。
org.hibernate.HibernateException: No CurrentSessionContext configured!
有点不解,虽然我的英语不好,但也知道这个的意思是没有当前的Session,但平时做的hiberante也是这样获得Session的,应该是没有错的,结果网上一查,原来这个只是个例。
解决的方法是配置sessionFactory中的参数
在集成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.没有错误了,。