使用 Hibernate 显示给定父节点的所有子节点
看来这个问题在编程中很流行。
我有一个表,其字段中有 Id 和 ParentId。
问题是,如何使用 Hibernate 正确处理像“检索给定节点的所有子节点”这样的任务
现在我得到了类似递归循环的东西,可以找到给定parentId的子节点,但表中的行数为10000行简直是疯了。
我所能发现的是,人们说这种数据库结构不足以容纳大量数据,但我已经走上了这条道路。除了用 10000 个小查询杀死我的数据库之外,我还能做什么?
It seems this problem is quite popular on programming.
I have a table that among its fields have Id and ParentId.
The question is, how to, using Hibernate, handle properly a task like "retrieve all children of a given node"
Right now I got something like a recursive loop that finds children given a parentId, but at 10000 rows in the table is just insane.
All I can find is that people say this DB structure is not adequate for big amounts of data, but I'm already on that path. What can I do other than killing my DB with 10000 small queries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 hibernate 对象具有一对多属性时,每次调用获取下一个子对象都会强制进行查询。这称为“延迟加载”,并通过按需获取它们来提高性能。您可以通过在映射上设置惰性属性来强制 Hibernate 在一个查询中“热切加载”所有集合。文档中提供了更多信息:
http://docs.jboss .org/hibernate/core/3.3/reference/en/html/performance.html#performance-fetching-lazyproperties
When a hibernate object has a one-to-many property, each call to get the next child forces a query. This is called "lazy loading" and increases performance by grabbing them on demand. You can force hibernate to "eagerly load" the set all in one query by setting the lazy property on your mapping. More information is available in the documentation:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-fetching-lazyproperties