在 NHibernate 中急切加载一棵树

发布于 2024-08-20 01:21:33 字数 115 浏览 3 评论 0原文

我在尝试加载树时遇到问题,这是我的情况,我有一个与自身关联的实体(层次结构),具有 n 个级别;问题是,我可以使用 ICriteria 或 HQL 急切地加载整个树吗?

预先感谢您的任何帮助。 爱丽儿

I have a problem trying to load a tree, this is my case, I have an entity associated with itself (Hierarchic) with n levels; the question is, Can I load eagerly the entire tree using ICriteria or HQL?

Thanks in advance for any help.
Ariel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

天暗了我发光 2024-08-27 01:21:33

是的...只需设置正确的获取模式即可。


我将在一分钟内提供示例。


示例取自此处 =>

IList cats = sess.CreateCriteria(typeof(Cat))
    .Add( Expression.Like("Name", "Fritz%") )
    .SetFetchMode("Mate", FetchMode.Eager)
    .SetFetchMode("Kittens", FetchMode.Eager)
    .List();

您也可以指定急切加载子级的子级=>

.SetFetchMode("Kittens.BornOn", FetchMode.Eager)

如果您使用 Linq to NHibernate,请使用 Expand 方法 =>

var feedItemQuery = from ad in session.Linq<FeedItem>().Expand("Ads")
                           where ad.Id == Id
                           select ad;

我建议使用辅助方法从传入的 lambda 表达式创建字符串。


很可能可以告诉 Criteria 加载整棵树。但我不知道这一点,我更喜欢指定我到底需要什么(加载所有内容似乎很危险)。


这个 有帮助吗?

Yes... just set correct fetchmode.


i'll include example in a minute.


Example taken from here =>

IList cats = sess.CreateCriteria(typeof(Cat))
    .Add( Expression.Like("Name", "Fritz%") )
    .SetFetchMode("Mate", FetchMode.Eager)
    .SetFetchMode("Kittens", FetchMode.Eager)
    .List();

You can specify to eager load child of child too =>

.SetFetchMode("Kittens.BornOn", FetchMode.Eager)

In case you are using Linq to NHibernate, use Expand method =>

var feedItemQuery = from ad in session.Linq<FeedItem>().Expand("Ads")
                           where ad.Id == Id
                           select ad;

And i would recommend to use helper method that creates string from passed in lambda expression.


Quite likely that it's possible to tell Criteria to load whole tree. But i'm not aware about that and i prefer specifying what exactly i need (seems dangerous to load everything).


Does this helps?

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