简体   繁体   English

Grails域类与自身的关系

[英]Grails domain class relationship to itself

I need a way to be able to have a domain class to have many of itself. 我需要一种能够让域类拥有许多功能的方法。 In other words, there is a parent and child relationship. 换句话说,有父母和子女的关系。 The table I'm working on has data and then a column called "parent_id". 我正在处理的表有数据,然后是一个名为“parent_id”的列。 If any item has the parent_id set, it is a child of that element. 如果任何项目设置了parent_id,则它是该元素的子项。

Is there any way in Grails to tell hasMany which field to look at for a reference? 在Grails中有什么方法可以告诉hasMany要查看哪个字段以供参考?

This is an example of what you are looking for (it's a snippet code I am running and it generates column parent_id). 这是您正在寻找的示例(它是我正在运行的代码段代码,它生成列parent_id)。 I don't think you need SortedSet: 我认为你不需要SortedSet:

class NavMenu implements Comparable { 
    String category
    int rank = 0

    String title
    Boolean active = false

    //NavMenu parent
    SortedSet subItems
    static hasMany = [subItems: NavMenu]
    static belongsTo = [parent: NavMenu]
  }

Furthermore, you can give name to the hasMany clause using the Mapping DSL, which is explained at http://grails.org/GORM+-+Mapping+DSL 此外,您可以使用Mapping DSL为hasMany子句指定名称,这在http://grails.org/GORM+-+Mapping+DSL中有说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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