NHibernate Join Fetch(Kind) [英] NHibernate Join Fetch(Kind)

查看:24
本文介绍了NHibernate Join Fetch(Kind)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个团队 -> 运动员关系并询问所有运动员.什么我对 fetch="Join" 有误解吗?这个映射是否应该导致要通过加入加载的团队?在迭代运动员时,它仍然懒惰地加载团队.

Given a Team -> Athlete relationship and querying all athletes. What am I misunderstanding about fetch="Join"? Should this mapping cause the Team to be loaded via a join? When iterating the athletes, it still lazy loads the Team.

public class AthleteMap : ClassMapping<Athlete>
{
        public AthleteMap()
        {
            ManyToOne(a => a.Team, o =>
                                       {
                                           o.Fetch(FetchKind.Join);
                                           o.Lazy(LazyRelation.NoLazy);
                                       }
                );    
        }    
}

产生此 HBM:

<class name="Athlete" table="Athletes">
    <id name="Id" type="Int32" />
    <property name="FirstName" />
    <property name="LastName" />
    <many-to-one name="Team" fetch="join" lazy="false" />
    <property name="Created" />
</class>

迭代:

var session = factory.OpenSession();

 foreach (var athlete in session.Query<Athlete>())
     Console.WriteLine("{0} {1}", athlete.FirstName, athlete.Team.Name); 

推荐答案

NHibernate Linq 查询不使用映射的获取策略.您必须像这样在 linq 查询中使用 Fetch().

The NHibernate Linq Query doesn't use the fetch strategy of the mapping. You have to Fetch() in your linq query like this.

var session = factory.OpenSession();

foreach (var athlete in session.Query<Athlete>().Fetch(x => x.Team))
   Console.WriteLine("{0} {1}", athlete.FirstName, athlete.Team.Name); 

映射文档中定义的获取策略影响:

The fetch strategy defined in the mapping document affects:

  • 通过 Get() 或 Load() 检索
  • 导航关联时隐式发生的检索
  • ICriteria 查询
  • HQL 查询是否使用了子选择提取

来源:性能获取

这篇关于NHibernate Join Fetch(Kind)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆