I have a domain class with a one-to-many association. It looks like so:
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
}
}
The domain class for FormResponseComment:
class FormResponseComment {
static mapWith = "mongo"
ObjectId id
Date dateCreated
Date lastUpdated
User createdBy
String comment
static belongsTo = [FormResponse]
static mapping = {
}
static constraints = {
}
}
I have a controller method for saving this object which looks like so:
def saveFormResponse(FormResponse formResponse) {
def saved = formService.saveFormResponse(formResponse)
respond(saved)
}
And the service method:
def saveFormResponse(response) {
return response.save(flush: true, failOnError: true)
}
When I post to this method, I can see the formResponseComments list populated as I expect it to be:
And the FormResponseComment is saved:
But the FormResponse object does not receive an association to the child FormResponseComment:
So why is the association not made here?
Grails 3.3.3.
Fixed by adding a back reference to the FormResponseComment domain class like so:
static belongsTo = [formResponse: FormResponse]