deamon
deamon

Reputation: 92377

Grails: eager loading doesn't work

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?

Upvotes: 2

Views: 1500

Answers (1)

Peter Ledbrook
Peter Ledbrook

Reputation: 4462

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!

Upvotes: 3

Related Questions