[英]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. 在数据库中,有一个带有关系ID的表,但我不知道如何访问它。
But that doesn't work, which is the proper way? 但这不起作用,这是正确的方法吗?
Thanks 谢谢
Change your Children class belongsTo clause : 更改您的Children类classesTo子句:
class Children{
static belongsTo = [parent: Parent]
}
This will allow you to access a child's parent instance : childInstance.parent
这将允许您访问孩子的父实例: 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和对父项的引用后,您只需
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.