Grails领域类与它自己的关系 [英] Grails domain class relationship to itself
问题描述
我需要一种能够让域类有很多自己的方法。换句话说,有一个父母和孩子的关系。我正在处理的表中有数据,然后是一个名为parent_id的列。如果任何项目设置了parent_id,则它是该元素的子项。
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". If any item has the parent_id set, it is a child of that element.
有没有什么方法可以让Grails告诉hasMany哪个字段需要引用?
Is there any way in Grails to tell hasMany which field to look at for a reference?
推荐答案
这是你正在寻找的一个例子(这是我正在运行的代码段代码,它会生成列parent_id)。我不认为你需要SortedSet:
This is an example of what you are looking for (it's a snippet code I am running and it generates column parent_id). I don't think you need 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]
}
另外,你可以使用Mapping DSL命名hasMany子句,在 http://grails.org/GORM+ - + Mapping + DSL
Furthermore, you can give name to the hasMany clause using the Mapping DSL, which is explained at http://grails.org/GORM+-+Mapping+DSL
这篇关于Grails领域类与它自己的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!