简体   繁体   English

Grails GORM,多层域类的渴望获取模式

[英]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 我已经修改了Bootstrap.groovy以在启动时初始化一些数据,并且单独调用Survey.list()Question.list()Response.list()显示每个单独的级别都是使用期望值创建的

However, when I do Survey.list() and drill into the questions, the responses are always null, like so : 但是,当我执行Survey.list()并深入研究问题时,响应始终为null,如下所示:

在此输入图像描述

I was expecting that by setting the fetchMode to eager it should always load that particular object. 我期待通过将fetchMode设置为急切,它应该始终加载该特定对象。

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? 我可以在我的域对象上更改什么,以确保当我执行Survey.findById(1)它会加载所有问题和响应?

Thanks 谢谢

please define this in your Survey class 请在Survey课程中定义

static fetchMode = [questions: 'eager']

if this don't work as fetchMode 'eager' is deprecated 如果这不起作用,则不推荐使用fetchMode'eager'

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 按照这个了解有关获取策略的更多信息https://grails.github.io/grails-doc/3.0.x/ref/Database%20Mapping/fetch.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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