我有一个具有一对多关联的域类。看起来像这样:
class FormResponse {
static String DRAFT = 'Draft'
static String SUBMITTED = 'Submitted'
static String REJECTED = 'Rejected'
static String APPROVED = 'Approved'
static mapWith = "mongo"
ObjectId id
Date dateCreated
Date lastUpdated
User createdBy
User updatedBy
Form form
String currentStatus = DRAFT
List<FormSectionResponse> formSectionResponses
List<FormResponseComment> formResponseComments
static hasMany = [ formSectionResponses: FormSectionResponse, formResponseComments: FormResponseComment ]
static mapping = {
}
static constraints = {
updatedBy nullable: true
}
}
FormResponseComment的域类:
class FormResponseComment {
static mapWith = "mongo"
ObjectId id
Date dateCreated
Date lastUpdated
User createdBy
String comment
static belongsTo = [FormResponse]
static mapping = {
}
static constraints = {
}
}
我有一个用于保存此对象的 Controller 方法,如下所示:
def saveFormResponse(FormResponse formResponse) {
def saved = formService.saveFormResponse(formResponse)
respond(saved)
}
以及服务方法:
def saveFormResponse(response) {
return response.save(flush: true, failOnError: true)
}
当我发布到此方法时,我可以看到formResponseComments列表已按预期填充:
并保存FormResponseComment:
但是FormResponse对象没有收到与子FormResponseComment的关联:
那么,为什么这里没有建立联系呢?
Grails 3.3.3。
最佳答案
通过向FormResponseComment域类添加向后引用来进行修复,如下所示:static belongsTo = [formResponse: FormResponse]