Why this No Session found for current thread exceptions in spring 3.1 and hibernate 4

前端 未结 3 1720
名媛妹妹
名媛妹妹 2021-01-03 13:32

I was trying to configure spring 3.1 and hibernate. I have written a simple test project. where I have user domain and userDao and its implementation

let me show yo

相关标签:
3条回答
  • 2021-01-03 13:42

    There are no transactions in your sample project. Add:

    <tx:annotation-driven />
    

    And annotate your DAO with @Transactional:

    @Transactional
    @Repository 
    public class UserDaoImpl implements UserDao
    

    Two notes:

    • you are correctly marking your DAO with @Repository, but it only handles exception translation:

      A class thus annotated is eligible for Spring DataAccessException translation

    • consider placing @Transactional on higher level classes (@Services), see What is the right way to use spring MVC with Hibernate in DAO, sevice layer architecture for discussion

    0 讨论(0)
  • 2021-01-03 13:53

    update to the latest jar. Their was jira created.

    https://jira.springsource.org/browse/SPR-8776

    0 讨论(0)
  • 2021-01-03 14:02

    You must set the propagation "REQUIRED" like this:

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    

    I do not know whether this is a spring bug.

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