Grails GORM Mongo hasMany 关联未保存
Grails GORM Mongo hasMany association not saved
我有一个具有一对多关联的域 class。看起来是这样的:
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:
class FormResponseComment {
static mapWith = "mongo"
ObjectId id
Date dateCreated
Date lastUpdated
User createdBy
String comment
static belongsTo = [FormResponse]
static mapping = {
}
static constraints = {
}
}
我有一个用于保存此对象的控制器方法,如下所示:
def saveFormResponse(FormResponse formResponse) {
def saved = formService.saveFormResponse(formResponse)
respond(saved)
}
及服务方式:
def saveFormResponse(response) {
return response.save(flush: true, failOnError: true)
}
当我 post 使用此方法时,我可以看到 formResponseComments 列表已按预期填充:
并保存 FormResponseComment:
但是 FormResponse 对象没有收到与子 FormResponseComment 的关联:
那么这里为什么没有关联呢?
Grails 3.3.3.
已通过添加对 FormResponseComment 域的反向引用来修复 class,如下所示:
static belongsTo = [formResponse: FormResponse]
我有一个具有一对多关联的域 class。看起来是这样的:
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:
class FormResponseComment {
static mapWith = "mongo"
ObjectId id
Date dateCreated
Date lastUpdated
User createdBy
String comment
static belongsTo = [FormResponse]
static mapping = {
}
static constraints = {
}
}
我有一个用于保存此对象的控制器方法,如下所示:
def saveFormResponse(FormResponse formResponse) {
def saved = formService.saveFormResponse(formResponse)
respond(saved)
}
及服务方式:
def saveFormResponse(response) {
return response.save(flush: true, failOnError: true)
}
当我 post 使用此方法时,我可以看到 formResponseComments 列表已按预期填充:
并保存 FormResponseComment:
但是 FormResponse 对象没有收到与子 FormResponseComment 的关联:
那么这里为什么没有关联呢?
Grails 3.3.3.
已通过添加对 FormResponseComment 域的反向引用来修复 class,如下所示:
static belongsTo = [formResponse: FormResponse]