beforeUpdate afterUpdate之前 [英] beforeUpdate afterUpdate

查看:829
本文介绍了beforeUpdate afterUpdate之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是否有beforeUpdateOf *(其中*是某个字段?)

以及另一个问题:


def beforeUpdate = {log.info(inUpUpdate+ this.status)}



def afterUpdate = {log.info(in afterUpdate+ this.status)}


这两者都给出相同的状态。
虽然实际上object(this)的状态从x更新为y

解决方案

改变了,但你可以添加一个明确的setter来执行某些操作:

  class MyDomainClass {
字符串状态

void setStatus(String status){
this.status = status
//根据改变后的值做一些事情
}
}

您在 beforeUpdate 和<$ c $中看到相同的值c> afterUpdate ,因为这些回调是在Hibernate将更改后的值保存到数据库时使用的。在Hibernate启动并完成更新的时间之间改变值是不寻常的。



如果您正在从数据库中查找原始值,则可以使用它使用 http://grails.org/doc/latest/ref/Domain% 20Classes / getPersistentValue.html


do we have beforeUpdateOf* (where * is some field?)

and another question :

def beforeUpdate= { log.info("in beforeUpdate " +this.status) }

def afterUpdate = { log.info("in afterUpdate " +this.status) }

This both gives same status. Although actually status of object(this) is updated from x to y

解决方案

There's no event for when a property is changed, but you could add in an explicit setter that does something:

class MyDomainClass {
   String status

   void setStatus(String status) {
      this.status = status
      // do something based on changed value
   }
}

You're seeing the same value in beforeUpdate and afterUpdate because those callbacks are for when Hibernate saves the changed values to the database. It would be unusual for the value to change between the time that Hibernate starts and finishes the update.

If you're looking for the original value from the database, it's available using http://grails.org/doc/latest/ref/Domain%20Classes/getPersistentValue.html

这篇关于beforeUpdate afterUpdate之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆