Grails:急切加载不起作用

发布于 2024-10-04 12:21:18 字数 705 浏览 1 评论 0原文

我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2024-10-11 12:21:18

从您的代码中尚不清楚,但我假设您这里有一个基于 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 on Organization non-lazy too.

This one will have to make it into the GORM Gotchas series!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文