У меня есть два класса домена:
Пользователь
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:"xxxyyyy@gmail.com",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
не удалось. Зачем? Где я Говронг?