简体   繁体   中英

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM