I'm working with this configuration in application.groovy:
grails.gorm.default.mapping = {
id (generator: "identity")
// send only the dirty fields to the database for updating
dynamicUpdate (true)
dynamicInsert (true)
}
The domain class:
package test
class Patient {
Integer id
Integer version
String familyName
String givenName
Patient father
Patient mother
static constraints = {
familyName (maxSize: 60)
givenName (maxSize: 60)
father ()
mother ()
}
static mapping = {
table 'patient'
father (column: "father_id", sqlType: "int4")
mother (column: "mother_id", sqlType: "int4")
}
def beforeUpdate () {
println('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
println('Record dirty? ' + this.isDirty())
println('Dirty properties: ' + this.dirtyPropertyNames)
println('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
}
}
bootstrap.groovy:
package test
class BootStrap {
def init = { servletContext ->
Patient father = new Patient(familyName: 'Patient', givenName: 'Father').save()
Patient mother = new Patient(familyName: 'Patient', givenName: 'Mother').save()
Patient child = new Patient(familyName: 'Patient', givenName: 'Child', father: father, mother: mother).save()
}
def destroy = {
}
}
- In browser click to see the Child
- Click Edit
- Don't change anything and click Update
- In the console you'll see, that noting is dirty but an update sql is being sent for version, father_id and mother_id - thought father_id and mother_id aren't being changed.
beforeUpdate XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Record dirty? false
Dirty properties: []
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
2019-08-07 13:04:03.936 DEBUG --- [nio-8080-exec-4]
org.hibernate.SQL :
/* update
test.Patient */ update
patient
set
version=?,
father_id=?,
mother_id=?
where
id=?
and version=?
2019-08-07 13:04:03.938 TRACE --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [1]
2019-08-07 13:04:03.938 TRACE --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [INTEGER] - [1]
2019-08-07 13:04:03.938 TRACE --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [INTEGER] - [2]
2019-08-07 13:04:03.939 TRACE --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [INTEGER] - [3]
2019-08-07 13:04:03.939 TRACE --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder : binding parameter [5] as [INTEGER] - [0]
--
Why's that so? How to disable the unnecessarily update? The foreign keys will be updated with each of an update of another simple property, even if they are staying the same?
(hmm... I don't see the button to upload a small example project...)
Many thanks and a wonderful day to you :-)