Grails 域类与自身的关系 [英] Grails domain class relationship to itself

查看:24
本文介绍了Grails 域类与自身的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来让域类拥有许多自身.换句话说,存在父子关系.我正在处理的表有数据,然后是一个名为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]
  }

此外,您可以使用映射 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆