Grails domain class relationship to itself

后端 未结 1 1303
天命终不由人
天命终不由人 2021-01-03 06:14

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 the

1条回答
  •  孤城傲影
    2021-01-03 06:40

    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]
      }
    

    Furthermore, you can give name to the hasMany clause using the Mapping DSL, which is explained at http://grails.org/GORM+-+Mapping+DSL

    0 讨论(0)
提交回复
热议问题