4

私は次のドメイン構造を持っています:

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)すべての質問と回答が確実に読み込まれるようになりますか?

ありがとう

4

1 に答える 1

0

これを 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

于 2015-10-27T10:01:30.530 に答える