我正在 application.groovy 中使用此配置:
grails.gorm.default.mapping = {
id (generator: "identity")
// send only the dirty fields to the database for updating
dynamicUpdate (true)
dynamicInsert (true)
}
领域类:
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')
}
}
引导程序.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 = {
}
}
- 在浏览器中单击以查看子项
- 单击编辑
- 不要更改任何内容并单击更新
- 在控制台中,您将看到,该注释很脏,但正在为版本、father_id 和 mother_id 发送更新 sql - 认为父亲 ID 和母亲 ID 没有被更改。
更新前 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
记录脏?错误的
脏属性:[]
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
2019-08-07 13:04:03.936 调试 --- [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] ohtype.descriptor.sql.BasicBinder:绑定参数 [1] 作为 [INTEGER] - [1]
2019-08-07 13:04:03.938 TRACE --- [nio-8080-exec-4] ohtype.descriptor.sql.BasicBinder:绑定参数 [2] 作为 [INTEGER] - [1]
2019-08-07 13:04:03.938 TRACE --- [nio-8080-exec-4] ohtype.descriptor.sql.BasicBinder:绑定参数 [3] 作为 [INTEGER] - [2]
2019-08-07 13:04:03.939 TRACE --- [nio-8080-exec-4] ohtype.descriptor.sql.BasicBinder:绑定参数 [4] 作为 [INTEGER] - [3]
2019-08-07 13:04:03.939 TRACE --- [nio-8080-exec-4] ohtype.descriptor.sql.BasicBinder:绑定参数 [5] 作为 [INTEGER] - [0]
--
为什么会这样?如何禁用不必要的更新?外键将随着另一个简单属性的每次更新而更新,即使它们保持不变?
(嗯……我没有看到上传小示例项目的按钮……)
非常感谢你,祝你有美好的一天:-)