无法使用save将对象保存到数据库(flush:true)

时间:2011-07-15 05:29:01

标签: grails

以下是我的简单域类:

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:false 家长:爸爸孩子:0

你能帮我理解为什么孩子总是0岁吗?我做错了什么?

1 个答案:

答案 0 :(得分:2)

应该是size()而不是count()

相关问题