如何以树形视图显示消息回复? Java&休眠
我有消息,需要显示十个(例如)第一个“根”回复以及对其自身的所有回复,并且它应该看起来像一棵树。 (标准消息和树状回复视图,你知道)。 所以,问题是 - 如何从数据库获取它 - 我正在使用休眠,据我所知,它将需要很多时间 - 递归地检索整个集合本身以及所有子树。 (而且,也许它只适用于小型集合,否则递归会导致堆栈溢出(哈哈。我们在这里:)) 有没有更有效的决策?
所以现在我有类似下面的代码,但我需要另一种方式(BaseEntry 是消息和任何回复的类):
@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;
...
您能给我一些建议吗?
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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个更有效的解决方案,但这意味着彻底改变数据库中存储内容的方式。阅读 http://mikehillyer.com/articles/managing-hierarchical-data-in -mysql/ 了解如何使用嵌套集树。这种方法使得写入成本高昂,但读取成本却低得多。如果您为已索引的根节点添加标志,则可以轻松获取根节点列表,然后获取子树。
我建议对他们的方法进行重大修改。他们使用完全没有间隙的整数集。这意味着每次写入都必须对树中的所有内容重新编号。这使得写入成本变得很多。但假设您以 2**20 的间隙开始根节点,并且默认情况下让每个子节点占用其可用空间的一半。然后,您无需进行任何重新编号,直到获得一组深度或宽度为 21 的回复。当您重新编号时,您可以仅对根节点下方的子树进行重新编号,因为您仍然有足够的间隙可以使用。
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.
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.