I'm trying to define a tree structure in GORM. Here is my model:
class Tree {
String name
Level rootLevel
static hasOne = [rootLevel: Level]
static hasMany = [levels: Level]
static mappedBy = [levels:"parentTree"]
}
class Level {
String name
Tree parentTree
Level parentLevel
Set<Level> subLevels
static belongsTo = [parentTree: Tree]
static hasMany = [subLevels: Level]
}
Insertion seems to work fine, but when I can't load a Tree with many levels and sublevels. I guess I missed something in the relations: - the Tree should have a reference to the rootLevel (and optionally to all sublevels) - a Level should have a reference to its parent level, its sublevels and the global parent Tree
Could you point me out the right direction to get a tree structure like this ? Thanks
I didn't like your tree structure, so i created my own :)
As for ensuring that all treenodes is loaded, you can always use eager/non-lazy fetching for each treeNode both parent and children. Could however have a performance penalty if your tree structures are very large...
As for eager/lazy fetching. take a look here: Using lazy property fetching in Grails / Gorm
I ended up with this solution (thanks to a friend):
and
I was missing the two relations between Tree and Level (owningTree and parentTree) and some mappedBy configuration to help hibernate.
Your problem seems to be that you have more than a single root node for each tree. This is an unusual approach. To make it work, you must replace
Level rootLevel
in the entityTree
withSet<Level> roots
.