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/