NHibernate SetFetchMode 不适用于嵌套条件

发布于 2024-12-15 17:34:11 字数 2450 浏览 2 评论 0原文

假设我运行以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愚人国度 2024-12-22 17:34:11

如果关联上有过滤器,那么 NH 无法确保返回关联的所有实体来初始化关联集合。您可以为 ids 发出 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>();

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

...
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>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文