Unable to save object to database with save(flush : true)
Here are my simple domain classes:
package family
class Parent {
static hasMany = [children : Child]
String name
}
package family
class Child {
static belongsTo = [parent : Parent]
String name
}
In the BootStrap I do following:
import family.Child;
import family.Parent;
class BootStrap {
def init = { servletContext ->
def parent = new Parent(name:'Dad')开发者_运维问答
parent.addToChildren(new Child(name:'son'))
parent.addToChildren([name : "another son"])
parent.save(flush : true, failOnError : true)
println "hasErrors: " + parent.hasErrors()
println "Parent: " + parent.name + " Children: " + parent.children.count()
}
def destroy = {
}
}
And in the console I see: hasErrors: false Parent: Dad Children: 0
Could you please help me to understand why children is always 0? What am I doing wrong?
It should be size()
not count()
.
精彩评论