GORM(grails)中的java树结构
我试图在GORM中定义一个树结构。这是我的模型:
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]
}
插入似乎很好,但当我无法加载一棵包含许多级别和子级别的树时。 我想我错过了关系中的一些东西: -树应该有一个对根级别(以及可选的所有子级别)的引用 -一个级别应该有一个对其父级、子级和全局父树的引用
你能给我指出一个正确的方向来建造这样的树结构吗? 谢谢
# 1 楼答案
我最终得到了这个解决方案(感谢一位朋友):
及
我忽略了树和级别(owningTree和parentTree)之间的两种关系,以及一些帮助休眠的mappedBy配置
# 2 楼答案
我不喜欢你的树结构,所以我创建了自己的:)
至于确保加载所有树节点,您始终可以对每个树节点(包括父节点和子节点)使用即时/非延迟抓取。但是,如果你的树结构非常大,可能会有性能损失
至于急切/懒惰的抓取。看看这里:Using lazy property fetching in Grails / Gorm