简体   繁体   English

没有调用Grails GORM beforeUpdate方法

[英]Grails GORM beforeUpdate method not invoked

I'm trying to fix an issue with my application where the beforeUpdate method in one of my domain classes is not invoked. 我正在尝试解决我的应用程序中一个域类中的beforeUpdate方法未调用的问题。

If none of the fields belonging directly to the domain class are updated, then the beforeUpdate event isn't fired. 如果没有直接属于域类的字段被更新,则不会触发beforeUpdate事件。 I guess this makes sense, however I would like the code in my beforeUpdate method to execute even in cases where only associations of my domain class are updated. 我想这是有道理的,但是即使在仅更新我的域类的关联的情况下,我也希望执行beforeUpdate方法中的代码。

For instance, my domain class looks something like this: 例如,我的域类看起来像这样:

class Organisation {
    String name
    Location location

    def beforeUpdate() {
        //do some stuff with the location when it's updated
    }
}

If the parameters map submitted from the form contains the name parameter, then beforeUpdate is fired. 如果从表单提交的参数映射包含name参数,则将beforeUpdate If the parameters map only contains location parameters, then it is not. 如果参数图仅包含location参数,则不包含。

Is there any way to make Grails fire beforeUpdate even if I'm only updating associations? 即使我仅更新关联,有什么方法可以使Grails在beforeUpdate

The beforeUpdate method is only fired when the object is actually updated, what you're probably seeing is that when only one parameter is submitted your domain instance is in an invalid state and doesn't validate, hence is never updated. 仅当对象实际更新时才触发beforeUpdate方法,您可能会看到,仅提交一个参数时,您的域实例处于无效状态且无法验证,因此永远不会更新。 What you are probably looking for to solve your case is the beforeValidate method. 您可能正在寻找的解决方案是beforeValidate方法。

The short answer to your question is: 您问题的简短答案是:

beforeUpdate will not fire unless one of the values of the properties on the domain class has changed. 除非域类上的属性值之一已更改,否则不会触发beforeUpdate

In your case beforeUpdate is not being called because no property of Organisation has changed. 在您的情况下,因为Organisation任何属性都没有更改,所以不会调用beforeUpdate

The only thing I can think to do in this situation is to change your domain model to allow Location to be aware of it's owning Organisation and manually call beforeUpdate from within the Location 's beforeUpdate . 我能想到在这种情况下做的唯一的事情就是改变你的域模型允许Location要知道它拥有的Organisation和手动调用beforeUpdate从内LocationbeforeUpdate All though, this feels hacky and smells bad. 尽管如此,这感觉很骇人,闻起来很不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM