Grails Hibernate Session Troubles - failed to lazily initialize a collection
Error:
Error 500: null
Servlet:
URI:
Exception Message: failed to lazily initialize a collection of role: Student.lockers, no session or session was closed
Caused by: Error executing tag <g:render>: Error executing tag <g:link>: failed to lazily initialize a collection of role: com.itzik.User.carts, no session or session was closed at grails-app/views/shared/_navigation.gsp:18 at grails-app/views/layouts/home.gsp:26
So I have a开发者_开发技巧 Student (with many Lockers). Now when a student logs in the security service handles putting them into the session. Then my main controller checks to see if they have an active locker. If not it creates one for them. Now for some reason it's choking on the Student –> Locker relationship.
You have several options:
use
fetch=FetchType.EAGER
- if the collection is not likely to contain too many objects, that in turn have other collections in them, this is the easiest option - you just fetch them from the database together with their owneruse OpenSessionInView - this would require some configuration time, but you might ultimately need it, even if not for this particular case. I can't tell you exactly how to configure OSIV in grails though.
use
Hibernate.initialize(collection)
before the session is closed (and before the transaction is committed) - this is manual, and pollutes the code, so less preferable
精彩评论