grails - Grails按 child 找 parent

标签 grails

我通过互联网浏览,但仍未找到问题的答案。应该很容易:

class Parent {
  String name
  Child child
}

当我有一个 child 对象时,如何获得它的 parent ?
喜欢:

def Parent = Parent.findByChild(child)

我怎样才能让它的 child 得到一个父对象?

谢谢

最佳答案

你去哪了

这些是 Realm 类吗?如果是这样,则可以通过has_many和belongs_to将它们连接:

class Parent {
    String name
    List children
    static has_many = [ children: Child ]
}

class Child {
   static belongs_to = [ parent: Parent ]
}

那你就可以写child.parent

关于grails - Grails按 child 找 parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7923510/

相关文章:

security - Grails 安全性

grails - grails:是否可以仅使用$ {}在gsp页面中进行条件标记

grails - 多个 when/then 与 where for each other

mysql - 字段 'class' 没有默认值

grails - Grails <g:actionSubmit>重定向到GSP而不是操作

java - 将整个目录内容复制到另一个目录?

Grails 自定义脚手架获取 Controller 名称的访问权限

mysql - GORM 中涉及 MySQL 函数 cos、弧度的复杂查询

具有复合键映射的 Grails/GORM 类和子类

grails - 如何重构Groovy中作为方法调用参数的闭包中的重复项?