1
我有兩個域類:驗證錯誤在域的建模,Grails的
用戶:
class User {
String login
String password
static belongsTo = [company: Company]
static hasMany = [holidays: Holiday]
static constraints = {
company(nullable:false)
}
}
公司:
class Company {
String shortName;
String name
Date dateCreated
String region
String email
Address address
Status status
Long tel
Long fax
static hasMany = [ users : User]
static constraints = {
name(nullable:false, size:6..20, unique:true)
shortName(size:2..10, nullable: true, unique:true)
region(nullable: true)
email(email:true)
tel(nullable: false, blank:false)
fax(blank:false)
address(nullable: false)
status(nullable: false)
users(nullable:true)
}
static mapping = {
address lazy: false
status lazy: false
}
}
我已經定義了一個一對多關係在User
和Company
之間。如果我寫這樣的測試:
void testCompanyHasManyUser() {
def user1 = new User(login:"anto", password:"secret")
assert user1.save(failOnError: true)
def company1 = new Company(shortName:"helloworld",name:"helloasd",region:"chennai",
email:"[email protected]",tel:23234,fax:343343, address: new
Address(country:"india",city:"chennai",street:"thayarst",code:"1233"),
status: new Status(name:"FIXED"))
assert company1.save()
company1.addToUsers(user1)
}
當我跑我得到的錯誤是這樣的測試:
Validation Error(s) occurred during save(): - Field error in object 'mnm.User' on field 'company': rejected value [null]; codes [mnm.User.company.nullable.error.mnm.User.company,mnm.User.company.nullable.error.company,mnm.User.company.nullable.error.mnm.Company,mnm.User.company.nullable.error,user.company.nullable.error.mnm.User.company,user.company.nullable.error.company,user.company.nullable.error.mnm.Company,user.company.nullable.error,mnm.User.company.nullable.mnm.User.company,mnm.User.company.nullable.company,mnm.User.company.nullable.mnm.Company,mnm.User.company.nullable,user.company.nullable.mnm.User.company,user.company.nullable.company,user.company.nullable.mnm.Company,user.company.nullable,nullable.mnm.User.company,nullable.company,nullable.mnm.Company,nullable]; arguments [company,class mnm.User]; default message [Property [{0}] of class [{1}] cannot be null]
grails.validation.ValidationException: Validation Error(s) occurred during save():
- Field error in object 'mnm.User' on field 'company': rejected value [null]; codes [mnm.User.company.nullable.error.mnm.User.company,mnm.User.company.nullable.error.company,mnm.User.company.nullable.error.mnm.Company,mnm.User.company.nullable.error,user.company.nullable.error.mnm.User.company,user.company.nullable.error.company,user.company.nullable.error.mnm.Company,user.company.nullable.error,mnm.User.company.nullable.mnm.User.company,mnm.User.company.nullable.company,mnm.User.company.nullable.mnm.Company,mnm.User.company.nullable,user.company.nullable.mnm.User.company,user.company.nullable.company,user.company.nullable.mnm.Company,user.company.nullable,nullable.mnm.User.company,nullable.company,nullable.mnm.Company,nullable]; arguments [company,class mnm.User]; default message [Property [{0}] of class [{1}] cannot be null]
at mnm.UserIntegrationTests.testCompanyHasManyUser(UserIntegrationTests.groovy:21)
我知道我在validation
部分犯了一個錯誤。儘管我保存了company
對象,但錯誤顯示user.save
失敗。爲什麼?我哪裏做錯了?
對不起已經編輯它 –
酷的問題依然存在,因爲你有一個驗證公司(可空:false),你不能保存沒有公司的用戶。要麼放寬驗證,或者如果它是正確的,在將用戶保存在測試 –
Ya中之前將該公司添加到用戶中,現在它可以工作。我感到困惑的是驗證部分,即使我刪除驗證「公司(可空:false)」部分,我也得到了和以前一樣的錯誤。任何想法爲什麼它出錯了? –