Grails:急切加载不起作用
我想从 main.gsp 中的用户对象访问组织列表:
<g:select name="effectiveOrganisation"
from="${session.user.organisations}" optionKey="id" optionValue="name"
value="${session.effectiveOrganisation?.id}" />
用户对象由以下类定义:
class SystemUser {
static hasMany = [organisations: Organisation]
static belongsTo = [Organisation]
static mapping = {
organisations lazy: false
}
}
但是当我执行代码时,我得到:
Exception Message: could not initialize proxy - no Session
Caused by: Error executing tag <g:form>:
Error executing tag <g:select>: could not initialize proxy - no Session
为什么 eager 在这里不起作用?
I want to access the list of organisations from a user object within the main.gsp:
<g:select name="effectiveOrganisation"
from="${session.user.organisations}" optionKey="id" optionValue="name"
value="${session.effectiveOrganisation?.id}" />
The user object is defined by the following class:
class SystemUser {
static hasMany = [organisations: Organisation]
static belongsTo = [Organisation]
static mapping = {
organisations lazy: false
}
}
But when I execute my code, I get:
Exception Message: could not initialize proxy - no Session
Caused by: Error executing tag <g:form>:
Error executing tag <g:select>: could not initialize proxy - no Session
Why does the eager not work here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的代码中尚不清楚,但我假设您这里有一个基于
belongsTo
属性的多对多。我已经成功地用 Grails 1.3.5 重现了这一点。该问题似乎只影响关系中具有
belongsTo
属性的一方。如果您尝试使用组织相同的代码 ->相反,它会起作用。修复方法相当奇怪:使
Organization
上的users
集合也变得非惰性。这个必须进入 GORM 陷阱系列!
It's not clear from your code, but I'm going to assume that you have a many-to-many here based on the
belongsTo
property.I've managed to reproduce this with Grails 1.3.5. The problem only seems to affect the side of the relationship that has the
belongsTo
property. If you tried the same code with organization -> users instead, it would work.The fix is rather odd: make the
users
collection onOrganization
non-lazy too.This one will have to make it into the GORM Gotchas series!