【问题标题】:Eager loading a tree in NHibernate渴望在 NHibernate 中加载树
【发布时间】:2011-01-11 04:35:24
【问题描述】:

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

提前感谢您的帮助。 爱丽儿

【问题讨论】:

    标签: c# nhibernate eager-loading


    【解决方案1】:

    是的...只需设置正确的 fetchmode。


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


    from here 为例 =>

    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 加载整个树。但我不知道这一点,我更喜欢指定我到底需要什么(加载所有内容似乎很危险)。


    this 有帮助吗?

    【讨论】:

    • 我知道我可以使用 FetchMode 加载与实体相关的集合,但我想加载整个树,而不仅仅是下一级。使用你的方法我会做: .SetFetchMode("Children", FetchMode.Join) .SetFetchMode("Children.Children", FetchMode.Join) .SetFetchMode("Children.Children.Children", FetchMode.Join) 等
    猜你喜欢
    • 1970-01-01
    • 2014-01-25
    • 2013-05-06
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多