gpt4 book ai didi

nhibernate - NHibernate Join Fetch(种类)

转载 作者:行者123 更新时间:2023-12-04 01:33:46 26 4
gpt4 key购买 nike

给定一个团队->运动员关系并查询所有运动员。什么
我对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);

最佳答案

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

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

    关于nhibernate - NHibernate Join Fetch(种类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8232420/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com