NHibernate SetFetchMode 不适用于嵌套条件
假设我运行以下代码:
var placementCriteria = DetachedCriteria.For<ResidentialPlacementClientService>();
placementCriteria.Add(Restrictions.Le("StartDate", effectiveDate));
placementCriteria.Add(Restrictions.Ge("EndDate", effectiveDate));
placementCriteria.SetFetchMode("CaseClient.CaseFile", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.Incomes", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.ClientProfile", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.ClientProfile.Person", FetchMode.Eager);
placementCriteria.SetFetchMode("VendorService", FetchMode.Eager);
placementCriteria.SetFetchMode("VendorService.Vendor", FetchMode.Eager);
using (var session = OpenSession())
{
using (var tx = session.BeginTransaction())
{
residentialPlacements = placementCriteria.GetExecutableCriteria(session).List<ResidentialPlacementClientService>();
}
}
我的“收入”属性已正确填充。
现在,当我添加收入标准时,该房产不再被急切地获取。
var placementCriteria = DetachedCriteria.For<ResidentialPlacementClientService>();
placementCriteria.Add(Restrictions.Le("StartDate", effectiveDate));
placementCriteria.Add(Restrictions.Ge("EndDate", effectiveDate));
placementCriteria.SetFetchMode("CaseClient.CaseFile", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.Incomes", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.ClientProfile", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.ClientProfile.Person", FetchMode.Eager);
placementCriteria.SetFetchMode("VendorService", FetchMode.Eager);
placementCriteria.SetFetchMode("VendorService.Vendor", FetchMode.Eager);
var caseClientCriteria = placementCriteria.CreateCriteria("CaseClient", JoinType.InnerJoin);
var incomesCriteria = caseClientCriteria.CreateCriteria("Incomes", JoinType.InnerJoin);
incomesCriteria.Add(Restrictions.Le("StartDate", effectiveDate));
incomesCriteria.Add(Restrictions.Or(Restrictions.Ge("EndDate", effectiveDate),
Restrictions.IsNull("EndDate")));
incomesCriteria.Add(Restrictions.Eq("IncomeType", incomeType));
using (var session = OpenSession())
{
using (var tx = session.BeginTransaction())
{
residentialPlacements = placementCriteria.GetExecutableCriteria(session).List<ResidentialPlacementClientService>();
}
}
NHibernate 为什么要这样做?如何更改我的查询/条件以便急切地获取该属性?
谢谢。
Assuming I run the following code:
var placementCriteria = DetachedCriteria.For<ResidentialPlacementClientService>();
placementCriteria.Add(Restrictions.Le("StartDate", effectiveDate));
placementCriteria.Add(Restrictions.Ge("EndDate", effectiveDate));
placementCriteria.SetFetchMode("CaseClient.CaseFile", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.Incomes", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.ClientProfile", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.ClientProfile.Person", FetchMode.Eager);
placementCriteria.SetFetchMode("VendorService", FetchMode.Eager);
placementCriteria.SetFetchMode("VendorService.Vendor", FetchMode.Eager);
using (var session = OpenSession())
{
using (var tx = session.BeginTransaction())
{
residentialPlacements = placementCriteria.GetExecutableCriteria(session).List<ResidentialPlacementClientService>();
}
}
The "Incomes" property on my is populated correctly.
Now when I add criteria on incomes, the property is no longer being fetched eagerly.
var placementCriteria = DetachedCriteria.For<ResidentialPlacementClientService>();
placementCriteria.Add(Restrictions.Le("StartDate", effectiveDate));
placementCriteria.Add(Restrictions.Ge("EndDate", effectiveDate));
placementCriteria.SetFetchMode("CaseClient.CaseFile", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.Incomes", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.ClientProfile", FetchMode.Eager);
placementCriteria.SetFetchMode("CaseClient.ClientProfile.Person", FetchMode.Eager);
placementCriteria.SetFetchMode("VendorService", FetchMode.Eager);
placementCriteria.SetFetchMode("VendorService.Vendor", FetchMode.Eager);
var caseClientCriteria = placementCriteria.CreateCriteria("CaseClient", JoinType.InnerJoin);
var incomesCriteria = caseClientCriteria.CreateCriteria("Incomes", JoinType.InnerJoin);
incomesCriteria.Add(Restrictions.Le("StartDate", effectiveDate));
incomesCriteria.Add(Restrictions.Or(Restrictions.Ge("EndDate", effectiveDate),
Restrictions.IsNull("EndDate")));
incomesCriteria.Add(Restrictions.Eq("IncomeType", incomeType));
using (var session = OpenSession())
{
using (var tx = session.BeginTransaction())
{
residentialPlacements = placementCriteria.GetExecutableCriteria(session).List<ResidentialPlacementClientService>();
}
}
Why does NHibernate do this? How can I change my query/criteria so that the property is fetched eagerly?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果关联上有过滤器,那么 NH 无法确保返回关联的所有实体来初始化关联集合。您可以为 ids 发出 Select,然后急切地获取具有关联的实体
if you have filters on an association then NH cant be sure that all entities of the association are returned to initialize the association collection. you can issue a Select for the ids and then fetch the entities with associations eagerly