用ssh整合时,用sessionfactory的getCurrentSession()获取不到session

在用ssh整合时,一开始用的是getCurrentSession(),获取当前线程上的session,但是总是抛异常,不能获取。

后来用sessionfactory的openSession(),但是,会发现一旦程序多次运行就会报错,提示没得session可获取。

那么怎么解决呢。

在beans.xml里面的hibernateProperties配置hibernate.current_session_context_class=org.springframework.orm.hibernate3.SpringSessionContext,class可根据自己实际运用的包而定。并且在service里面加上事务注解@Transactional。问题就能解决了。

原文地址:https://www.cnblogs.com/qldhlbs/p/4772665.html