Grails domain class relation to itself

I need a way to have a domain class in order to have many of them. In other words, there are relationships between parents and children. The table I'm working in has data, and then a column called "parent_id". If any element has a parent set, it is a child of this element.

Is there any way in Grails to tell hasMany which field to look for for a link?

+4
source share
1 answer

This is an example of what you are looking for (this is the code of the fragment that I run, and it generates the parent_id column). I don't think you need a 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] } 

Alternatively, you can specify a name for the hasMany clause using the DSL mapping, which is explained in http://grails.org/GORM+-+Mapping+DSL

+6
source

All Articles