您是否可以强制Grails中的DomainClass.get(id)功能从数据库中提取新实例,而不是使用之前在会话中检索到的本地副本?
我想比较实例的属性是否已更改:
示例:
def myInstance = MyClass.get(params.id)
myInstance.properties = params.data // updates the instance, but does not save it
// stuff happens
def myDbInstance = MyClass.get(params.id) // would like this to pull a fresh copy from the database, unchanged to allow for comparison
if(myDbInstance.value != myInstance.value) {
// do something if has changed
}
谢谢。
答案 0 :(得分:0)
使用可以使用refresh()方法强制从数据库重新加载实例。
答案 1 :(得分:0)
如果我们想要将视图中更新的字段与数据库中已有的字段进行比较,refresh()
将更新尚未保存在数据库中的实例。
在这种情况下,实例上的getPersistentValue('field_name')
将获取数据库中的值,并可用于比较。
如果控制器的更新操作中已有employeeAllocationInstance
可用,例如,
我们想比较更新的字段,让我们说jan
分配
然后
existing_allocation = EmployeeAllocation.get(employeeAllocationInstance.id)
会更新employeeAllocationInstance
中的值,以便我们可以
existing_allocation?.getPersistentValue('jan')?.value
获取存储在数据库中的值(尚未更新)