I would like to create a grails domain class that links to itself.
This related post suggests a solution but I can’t get it to work: Grails domain class relationship to itself
For one thing I don’t understand what comparable does and would need to add a int compareTo(obj) method.
Adding the following to my code without implementing Comparable compiles, but grails crashes at runtime:
//NavMenu parent
SortedSet subItems
static hasMany = [subItems: NavMenu]
static belongsTo = [parent: NavMenu]
static constraints = { parent(nullable:true) }
Thanks in advance
When you’re using
SortedSet, a sort algorithm is internally executed, but it needs a sort criteria. You need to implement theComparableinterface because that is the standard way to provide a sort criteria to the internal algorithm.If you don’t need a specific order, you can delete the
SortedSet subItemsline and thus avoid implementing theComparableinterface.