【发布时间】:2011-04-16 20:01:41
【问题描述】:
我正在为留言板实现一个自引用 GORM 对象。到目前为止,伪 GORM 类:
class Article {
String title
Article parent
static belongsTo = [parent: Article]
static hasMany = [children: Article]
static constraints = {
parent(nullable: true)
}
static transients = ['descendants', 'ancestors']
def getDescendants() {
return children ? children*.descendants.flatten() + children : []
}
def getAncestors() {
return parent ? parent.ancestors.flatten() + this : []
}
}
所以,这在我的本地盒子上运行良好,但我担心它是否会在现场扩展,每天有数千个唯一用户。
自从 Burt Beckwith 的演讲 http://www.infoq.com/presentations/GORM-Performance 之后,我就倾向于不使用 hasMany / belongsTo。
主要是阅读消息与添加新消息。
我可以缓存 getDescendants 和 getAncestors 调用。
我可以添加一个名为“hasChildren”的新布尔值。该字段可以通过覆盖 addToChildren 和 removeFromChildren 方法进行操作。使用“hasChildren”可以防止像
这样的事情if (article.children.size() > 0) // show replies
改为:
if (article.hasChildren) // show replies
想法?有什么建议吗?
提前致谢, 托德
【问题讨论】:
-
嗨,我也遇到了同样的问题。有更新吗?
标签: grails grails-orm