[英]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.