繁体   English   中英

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

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

我有以下域结构:

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
    }
}

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

但是,当我执行Survey.list()并深入研究问题时,响应始终为null,如下所示:

在此输入图像描述

我期待通过将fetchMode设置为急切,它应该始终加载该特定对象。

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

谢谢

请在Survey课程中定义

static fetchMode = [questions: 'eager']

如果这不起作用,则不推荐使用fetchMode'eager'

你也可以试试

static mapping = {
 questions fetch :'join'
}

按照这个了解有关获取策略的更多信息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