Grails保存方法给出错误拒绝值

时间:2019-05-17 12:01:26

标签: java hibernate grails

当我保存一个User对象时,它给了我以下错误

我的保存代码

User user = new User();
user.surname = "Test";
user.name = "Manoj";
user.save();

验证错误

grails.validation.ValidationException: Validation error whilst flushing entity 
[User]:
- Field error in object 'User' on field 'surname': rejected value [null]; codes [User.surname.nullable.error.User.surname,User.surname.nullable.error.surname,User.surname.nullable.error.java.lang.String,User.surname.nullable.error,stream.surname.nullable.error.User.surname,stream.surname.nullable.error.surname,stream.surname.nullable.error.java.lang.String,stream.surname.nullable.error,User.surname.nullable.User.surname,User.surname.nullable.surname,User.surname.nullable.java.lang.String,User.surname.nullable,stream.surname.nullable.User.surname,stream.surname.nullable.surname,stream.surname.nullable.java.lang.String,stream.surname.nullable,nullable.User.surname,nullable.surname,nullable.java.lang.String,nullable]; arguments [surname,class User]; default message [Property [{0}] of class [{1}] cannot be null]

用户类别:

class User {
    String surname
    String name
} 

由于姓氏为必填字段,因此我无法将null设置为true。

1 个答案:

答案 0 :(得分:0)

如果这是现有数据库的类,并且未正确映射,则可能解释该错误。

之所以这样说,是因为我没有看到约束或映射。