转 使用Hibernate操作数据库时报:No CurrentSessionContext configured! 异常

没有currentSession配置错误,即在我们使用currentSession的时候要在hibernate.cfg.xml中进行相关的事务配置:
1、本地事务
<property name="hibernate.current_session_context_class">thread</property>
2、全局事务
<property name="hibernate.current_session_context_class">jta</property>

这样就解决了!

如果在获得session 对象时使用的是 session = sessionFactory.getCurrentSession();

则此处可以改成 session = sessionFactory.openSession();

这样就不用修改上面的配置文件了