NHibernate Join Fetch(Kind)

时间:2011-11-22 19:13:10

标签: nhibernate nhibernate-mapping

鉴于团队 - >运动员关系和查询所有运动员。什么 我误解了fetch="Join"吗?该映射是否应该导致 通过加入加载团队?当迭代运动员时,它 仍懒得加载团队。

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); 

1 个答案:

答案 0 :(得分:13)

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

var session = factory.OpenSession();

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

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

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

源:performance-fetching