这是我的简单域类:
package family
class Parent {
static hasMany = [children : Child]
String name
}
package family
class Child {
static belongsTo = [parent : Parent]
String name
}
在BootStrap中,我执行以下操作:
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 = {
}
}
在控制台中,我看到:
hasErrors:否
parent :父亲 child :0
您能帮我理解为什么 child 总是0吗?我究竟做错了什么?
最佳答案
应该是size()
而不是count()
。
关于grails - 无法使用save(flush:true)将对象保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6703034/