Grails: 404 and lazy initialization
If you visit a page that does not exist, and a 404 is "thrown" and Grails goes to try to render my custom 404 error page, I get lazy initialization errors from some taglibs I have that are executed in the error layout.
The taglib does in fact try to a开发者_运维知识库ccess a domain object inside of another domain object that is a has-many relationship. It is able to get the initial object, but when accessing the relationship, it throws this lazy initialization error.
However, this does not happen when any other page is accessed properly. Even if I try to access my custom error page directly. Only when a 404 error is thrown.
Is there something different about the hibernate session during this time that causes the lazy initialization error?
Is it possible that accessing something that is lazily initialized in a taglib is not a good idea in Grails?
I've seen this before, and my usual solution would be to move the querying to the controller. I believe this is due to the fact that the controller and GSP sessions are not one and the same - hence, it's a good idea to do most database reads within a controller.
In this case, there is no controller.
As far as hacks to get around this problem ...
I a wondering if you can't start a session/end a session within your GSP and/or taglib to force the behavior for now. Another idea is to force a read of the relationship at the same time you access the original (parent) object - this will initialize the lazy relationship.
精彩评论