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

问题描述

建立团队->运动员关系并查询所有运动员.什么我对fetch="Join"有误解吗?该映射是否应引起通过联接加载团队?在对运动员进行迭代时仍然懒惰地加载团队.

Given a Team -> Athlete relationship and querying all athletes. Whatam I misunderstanding about fetch="Join"? Should this mapping causethe Team to be loaded via a join? When iterating the athletes, itstill 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(种类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 16:26
查看更多