简体   繁体   中英

Grails GORM, eager fetch mode for multiple layers of domain class

I have the following domain structure :

class Survey {

    Integer id
    String title

    static hasMany = [questions: Question]
    static constraints = {
        id()
        title()
        questions()
    }

    String toString(){
        return title
    }
}

class Question {

    Integer id
    String text

    static hasMany = [responses: Response]
    static fetchMode = [responses: 'eager']

    static constraints = {
        id()
        text()
        responses()
    }

    String toString(){
        return text
    }
}

class Response {

    Integer id
    String text
    Integer numberOfPeopleSelected = 0

    static constraints = {
        id()
        text()
        numberOfPeopleSelected()
    }

    String toString(){
        return text
    }
}

I've modified Bootstrap.groovy to initialise some data on startup, and separate calls to Survey.list() , Question.list() and Response.list() show that each individual level is created with expected values

However, when I do Survey.list() and drill into the questions, the responses are always null, like so :

在此输入图像描述

I was expecting that by setting the fetchMode to eager it should always load that particular object.

What can I change on my domain objects to ensure that when I do something like Survey.findById(1) it loads all the questions and responses?

Thanks

please define this in your Survey class

static fetchMode = [questions: 'eager']

if this don't work as fetchMode 'eager' is deprecated

you can also try

static mapping = {
 questions fetch :'join'
}

follow this to know more on fetch strategy https://grails.github.io/grails-doc/3.0.x/ref/Database%20Mapping/fetch.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM