I have two domain classes:
User:
class User {
String login
String password
static belongsTo = [company: Company]
static hasMany = [holidays: Holiday]
static constraints = {
company(nullable:false)
}
}
Company:
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
}
}
I have defined a one-many relationship between User and Company. And if I write the test like this :
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)
}
When I run the test I’m getting the error like this:
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)
I know I have made an error in validation part. Even though I’m saving the company object, the error shows that the user.save fails. Why? Where did I gowrong?
The problem looks to be that you say users must have a company, then create a user with no company and try to save it.
Try setting a company before calling
However, you have shown the test for
testCompanyHasManyUser, and the error fromtestUserAddingHoliday, so it’s hard to be sure