GORM 中的树结构(grails) [英] Tree structure in GORM (grails)
问题描述
我正在尝试在 GORM 中定义树结构.这是我的模型:
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]
}
插入似乎工作正常,但是当我无法加载具有多个级别和子级别的树时.我想我错过了一些关系:- 树应该有一个对 rootLevel 的引用(以及可选的所有子级别)- 一个关卡应该有一个对它的父关卡、它的子关卡和全局父树的引用
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 ended up with this solution (thanks to a friend):
class Tree {
String name
Level rootLevel
static hasMany = [levels: Level]
static mappedBy = [rootLevel: "parentTree", levels: "owningTree"]
static constraints = {rootLevel(nullable: true)}
}
和
class Level {
String name
Tree parentTree
Tree owningTree
Level parentLevel
Set<Level> subLevels
static belongsTo = [owningTree: Tree, parentLevel: Level]
static hasMany = [subLevels: Level]
static mappedBy = [parentTree: "rootLevel", owningTree: "levels", subLevels: "parentLevel"]
static constraints = {
parentTree(nullable: true)
parentLevel(nullable: true)
}
}
我遗漏了 Tree 和 Level 之间的两个关系(owningTree 和 parentTree)以及一些有助于休眠的mappedBy 配置.
I was missing the two relations between Tree and Level (owningTree and parentTree) and some mappedBy configuration to help hibernate.
这篇关于GORM 中的树结构(grails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!