开发者

Grails: eager loading doesn't work

开发者 https://www.devze.com 2023-01-27 14:53 出处:网络
I want to access the list of organisations from a user object within the main.gsp: <g:select name=\"effectiveOrganisation\"

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 = [Organi开发者_开发知识库sation]
    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?


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消