NHibernate SetFetchMode不能与嵌套条件一起工作
本文关键字:条件 一起 工作 嵌套 SetFetchMode 不能 NHibernate | 更新日期: 2023-09-27 18:14:38
假设我运行以下代码:
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>();
}
}
my的"收入额"属性被正确填充。
现在,当我添加收入标准时,属性不再被急切地获取。
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这样做?我怎样才能改变我的查询/条件,以便急切地获取属性?
谢谢。
如果在关联上有过滤器,那么NH不能确保返回关联的所有实体来初始化关联集合。你可以对id发出Select,然后急切地获取具有关联的实体
...
placementCriteria.SetProjection(Projections.Id());
residentialPlacements = session.CreateCriteria<ResidentialPlacementClientService>()
.Add(Subqueries.In("Id", placementCriteria))
.SetFetchMode("CaseClient.CaseFile", FetchMode.Eager);
.SetFetchMode("CaseClient.Incomes", FetchMode.Eager);
.SetFetchMode("CaseClient.ClientProfile", FetchMode.Eager);
.SetFetchMode("CaseClient.ClientProfile.Person", FetchMode.Eager);
.SetFetchMode("VendorService", FetchMode.Eager);
.SetFetchMode("VendorService.Vendor", FetchMode.Eager);
.List<ResidentialPlacementClientService>();