No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

前端 未结 9 669
情歌与酒
情歌与酒 2020-12-23 17:03

I am getting this exception when I call a DAO method which uses SessionFactory.getCurrentSession(). The DAO class is annotated with @Transactional

相关标签:
9条回答
  • 2020-12-23 18:03

    Have you got org.springframework.orm.hibernate3.support.OpenSessionInViewFilter configured in webapp's web.xml (assuming your application is a webapp), or wrapping calls accordingly?

    0 讨论(0)
  • 2020-12-23 18:09

    I resolved this by adding @Transactional to the base/generic Hibernate DAO implementation class (the parent class which implements the saveOrUpdate() method inherited by the DAO I use in the main program), i.e. the @Transactional needs to be specified on the actual class which implements the method. My assumption was instead that if I declared @Transactional on the child class then it included all of the methods that were inherited by the child class. However it seems that the @Transactional annotation only applies to methods implemented within a class and not to methods inherited by a class.

    0 讨论(0)
  • 2020-12-23 18:09

    I was getting the same the error inside a shared function, but it was only happening for some calls to this shared function. I eventually realized that one of classes calling the shared function wasn't wrapping it inside of a Unit of Work. Once I updated this classes functions with a Unit of Work everything worked as expected.

    So just posting this for any future visitors who run into this same error, but for whom the accepted answer doesn't apply.

    0 讨论(0)
提交回复
热议问题