nhibernate 获取整棵树
我需要帮助使用 nhibernate 3.0 从数据库获取树层次结构,
QueryOver.Of<Enterprise>(() => entAlias)
.JoinAlias(() => entAlias.ChildEntities, () => childEntityAllias, JoinType.LeftOuterJoin)
.TransformUsing(new DistinctRootEntityResultTransformer())
我只得到图形的两个级别(父级及其子级),而不是子级的子级等。
如果我尝试获取叶子的父级,情况也是如此。我只得到叶子的父级,而不是叶子的父级的父级的父级。其中级别 = n。
何做这种类型的查询。无论是 Icriteria、linq、HQL 还是其他。
I need help fetching tree hierarchy from db using nhibernate 3.0
QueryOver.Of<Enterprise>(() => entAlias)
.JoinAlias(() => entAlias.ChildEntities, () => childEntityAllias, JoinType.LeftOuterJoin)
.TransformUsing(new DistinctRootEntityResultTransformer())
I am getting only the two level of the graph (parent and its childrens) but not the childrens of children etc.
The same is if I try to fetch parents of the leaf. I get only the parent of the leaf, but not parent of the parent of the parent... of the leaf. where level = n.
Ho to do this type of query. no mather if Icriteria, linq, HQL or else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用批量大小来更有效地获取子项。
子项仍然由单独的查询加载(您不应该再将它们加入到查询中),但总是一次加载 20 个。将“N+1”变为“N/20+1”,通常效果很好。这个解决方案的优点是你不需要在查询中关心。
您还可以批量加载父级:
如果可能,它会批量加载与企业的多对一关系。
如果您需要更多优化,请考虑添加对根(最顶层父级)的引用。然后,您可以通过一个简单的查询加载根的所有子项。缺点是你需要关心这个引用,它是一个冗余,也可能很难维护。
You could use batch-size to fetch the children more efficiently.
Children are still loaded by separate queries (you shouldn't join them in the query anymore), but always 20 at once. To turns "N+1" to "N/20+1" which usually performs very good. The advantage of this solution is that you don't need to care about in your queries.
You could also load parents in batches:
It loads many-to-one relations to Enterprises in batches if possible.
If you need even more optimization, consider to add a reference to the root (the top most parent). Then you can load all the children of a root in one easy query. The disadvantage is that you need to care about this reference, it is a redundancy which could also be hard to maintain.
如果你真的想获取所有内容(尽管我不确定你为什么要这样做),那么在 NHibernate 中禁用延迟加载(从而启用急切加载)。
If you really want to fetch everything (although I'm not sure why you would do such a thing) then disable lazy loading (thus enabling eager loading) in NHibernate.