簡體   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