Search code examples
grailsforeign-keysgrails-orm

Find Parent By Child One to Many Relation Grails


I have this classes:

class Parent{
    static hasMany = [children:Child]
}

class Children{
    static belongsTo = [Parent]
}

I want to do something like

Parent.findByChildren(ChildInstance)

In the database there is a table with the relationship id's, but I don't know how to access to it.

But that doesn't work, which is the proper way?

Thanks


Solution

  • Change your Children class belongsTo clause :

    class Children{
        static belongsTo = [parent: Parent]
    }
    

    This will allow you to access a child's parent instance : childInstance.parent