gpt4 book ai didi

mongodb - Grails GORM Mongo hasMany关联未保存

转载 作者:行者123 更新时间:2023-12-02 14:39:29 25 4
gpt4 key购买 nike

我有一个具有一对多关联的域类。看起来像这样:

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列表已按预期填充:

enter image description here

并保存FormResponseComment:

enter image description here

但是FormResponse对象没有收到与子FormResponseComment的关联:

enter image description here

那么,为什么这里没有建立联系呢?

Grails 3.3.3。

最佳答案

通过向FormResponseComment域类添加向后引用来进行修复,如下所示:
static belongsTo = [formResponse: FormResponse]

关于mongodb - Grails GORM Mongo hasMany关联未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50938540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com