我有以下域结构:
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()
并深入研究问题时,响应始终为空,如下所示:
我期望通过设置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
关于Grails GORM,多层领域类的急切获取模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15532749/