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
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
First I would change the relationship in Children domain to
static belongsTo = [parent: Parent] // suggested by @bassmartin
or
Parent parent
both does the same thing.
Once you have the ChildInstance and the reference to the parent, you can simply do
ChildInstance.parent // returns instance of parent
Similarly, if you want to find all the children of a parent, you can do
parent.children // return an array of children which you can iterate over.
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.