grails - GORM在调用beforeInsert方法之前检查约束

标签 grails gorm aop

GORM的奇怪行为:

    public class Student {
        String fullname;
        String subscriptionIdentity;

       static constraints={

          subscriptionIdentity blank:false,nullable:true // mean that this field should have value in DB
      }

       def beforeInsert(){
         subscriptionIdentity="S"+System.nanoTime();
      }
    }

当我打电话时:
def std=new Student(fullname:"Ahmed OMAR")
std.save();

似乎GORM不会调用beforeInsert(或在检查约束后调用它)
因为我收到以下错误消息:

Field error in object 'com.abdennour.Student' on field 'subscriptionIdentity': rejected value [null];

最佳答案

这是因为.save()调用验证来确保实例有效。

在您的情况下,您可能需要使用beforeValidation而不是beforeInsert。您可以在documentation中阅读有关beforeValidation的更多信息。

关于grails - GORM在调用beforeInsert方法之前检查约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33866368/

相关文章:

mysql - mysql 的 maxSize 为 25000 的 grails 2.3.1 问题

hibernate - Grails 3.1.6,Hibernate4。复合外键

spring - 如何为多个包指定单个切入点

Grails 表或列表中的附加列

spring - Grails 应用程序每隔几个小时就会停止响应

grails - Grails渴望动态发现者

grails - 如何将域类与另一个域类的两个实例映射

c# - CaSTLe DynamicProxy 接口(interface)代理生成

java - 如何拦截类型或方法的元注释

java - 从基类继承 Grails 域类属性