Hibernate/Spring: Session still closed before retrieval of child collection even with @Transactional on Service method

后端 未结 2 1816
梦如初夏
梦如初夏 2021-01-25 12:55

Hello Spring/Hibernate Gurus!

in a time like this i would like you to be my best friends.I am working on a project using hibernate 3.6.1.Final JPA implementation using s

2条回答
  •  隐瞒了意图╮
    2021-01-25 13:36

    The scope of the transaction (and hence the session) is only around the getContentsbyCategoryID() method. Once you return from it, the transaction is committed and the session is closed. Where, exactly, is it throwing the exception? Is it on the list() operation inside getContentsbyCategoryID()? Or is it, in fact, after you have returned from getContentsbyCategoryID() and trying to access the lazy collection somewhere else in the code? In that case, you either have to

    1. increase the scope of the transaction (and, hence, the session)
    2. change fetch-type to eager
    3. manually load the collection in getContentsbyCategoryID() (by calling size() on it, for instance)
    4. Adopt the open-session-in-view pattern (either through the OpenSessionInViewFilter or the OpenSessionInViewInterceptor)

提交回复
热议问题