nhibernate 获取整棵树

发布于 2024-09-18 02:10:41 字数 433 浏览 2 评论 0原文

我需要帮助使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病女 2024-09-25 02:10:41

您可以使用批量大小来更有效地获取子项。

<bag name="Childen" batch-size="20" ...>

子项仍然由单独的查询加载(您不应该再将它们加入到查询中),但总是一次加载 20 个。将“N+1”变为“N/20+1”,通常效果很好。这个解决方案的优点是你不需要在查询中关心。

您还可以批量加载父级:

<class name="Enterprise" batch-size="20">

如果可能,它会批量加载与企业的多对一关系。

如果您需要更多优化,请考虑添加对根(最顶层父级)的引用。然后,您可以通过一个简单的查询加载根的所有子项。缺点是你需要关心这个引用,它是一个冗余,也可能很难维护。

You could use batch-size to fetch the children more efficiently.

<bag name="Childen" batch-size="20" ...>

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:

<class name="Enterprise" batch-size="20">

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.

请你别敷衍 2024-09-25 02:10:41

如果你真的想获取所有内容(尽管我不确定你为什么要这样做),那么在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文