如何显示树视图回复消息? Java&过冬 [英] How to show tree-view replies to message? Java & Hibernate

查看:147
本文介绍了如何显示树视图回复消息? Java&过冬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有消息,并且需要显示10个(例如)第一个根回复,其中包含对所有回复的回复,它应该看起来像一棵树。 (标准消息和树回复视图,你知道)。
所以,问题是 - 如何从DB获得它 - 我使用hibernate,并且它会花费大量的时间 - 递归地检索整个集合本身,以及所有子树。 (也许,它只适用于小型集合,否则递归会导致堆栈溢出(哈哈,这里我们是:))
是否有更高效的决策?

I have message, and need to show ten (for example) first 'root' replies with all the replies to itselfs, and it should look like a tree. (Standard messages and tree replies view, you know). So, the question is - how to get it from DB - i'm using hibernate, and afaik it will get a lot of time - to retrieve the WHOLE collection itself, with all subtrees, recursively. (And, maybe, its good only for small-sized collections, otherwise recursion will cause a stack overflow (Ha-ha. Here we are :) ) Is there a more efficient decision?

因此,现在我的代码如下代码,但我需要另一种方法(BaseEntry同时适用于消息和任何回复):

So now I have smth like code below, but I need another one way (BaseEntry is class for both the message and any reply) :

@Entity public class BaseEntry extends VersionedEntity {
private @Nullable BaseEntry parent;

@ManyToOne
@ForeignKey(name="base_entry_parent__base_entry_fk")
@Nullable public BaseEntry getParent()
{
    return parent;
}

@OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
private List<BaseEntry> children;

...

请问您有什么建议吗?

Could you advice something, please?

推荐答案

有一个更高效的解决方案,但它意味着完全改变你将数据存储在数据库中的方式。阅读 http://mikehillyer.com/articles/managing-hierarchical-data-in -mysql / 了解如何使用嵌套集树。这种方法使得写入成本很高,但读取得多,便宜得多。如果你添加了一个你已经建立了索引的根节点标志,那么很容易获取你的根节点列表,然后获取子树。

There is a more efficient solution, but it will mean completely changing how you store things in the database. Read http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ to learn how to use nested set trees. That approach makes writes expensive, but reads much, much cheaper. If you add in a flag for root nodes that you have indexed, then it is easy to fetch your list of root nodes, then fetch the subtrees.

修改,我会建议他们的方法。他们使用完全没有空白的整数集合。这意味着每个写入都必须重新编号树中的所有内容。这使得写入很多更加昂贵。但是,假设您以2 ** 20的间距开始根节点,并且默认情况下每个子节点占用一半的可用空间。然后,你不必做任何重新编号,直到你有一组21或更深的答复。当你重新编号时,你可以重新编号在根节点下面的子树,因为你仍然有很多差距要使用。

There is a significant modification that I would suggest to their approach. They used sets of integers with no gaps at all. This means that every write has to renumber everything in the tree. This makes writes a lot more expensive. But suppose that you start root nodes out with gaps of 2**20 instead, and have each child by default take up half the space available to it. Then you don't have to do any renumbering at all until you have a set of replies that is 21 deep or wide. And when you do renumber, you can renumber just the subtree below that root node, because you still have plenty of gaps to use.

这篇关于如何显示树视图回复消息? Java&amp;过冬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆