NHibernate 对左外连接进行过滤

发布于 2024-12-09 18:11:28 字数 223 浏览 1 评论 0原文

我遇到了 NHibernate 过滤器的问题。它工作得很好,直到我对对象进行左外连接。

例如,Deal 引用PurchaseItem,但PurchaseItem 应用了CompanyId 筛选器。

如果我想查询处理与PurchaseItem 的左连接,CompanyId 过滤器将应用在WHERE 子句中,导致不会返回任何内容。

有没有办法在连接而不是在 where 子句应用过滤器?

I am running into an issue with an NHibernate filter. It works great until I do a left outer join to the object.

For example, Deal references PurchaseItem, but PurchaseItem has a CompanyId filter applied.

If I want to query deal with a left join to PurchaseItem, the CompanyId filter is applied in the WHERE clause, causing nothing to return.

Is there a way to apply the filter at the join instead of at the where clause?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

后eg是否自 2024-12-16 18:11:28

通过使用此过滤条件修复了此问题:
“CompanyId is NULL or CompanyId = :companyId”

这是可以的,因为 CompanyId 在 SQLServer 中不为空,所以如果我们进行外连接,我们应该只得到 NULL。

Fixed this by using this filter condition:
"CompanyId is NULL or CompanyId = :companyId"

This is ok because CompanyId is not-null in SQLServer so we should only get NULL if we are doing an outer join.

半边脸i 2024-12-16 18:11:28

我正在寻找可空可选字段的确切问题

请注意,您的 OR 将避免使用索引,因此可能会更慢。

更新:我想我找到了解决办法:

                // register filters
                var filterDef = new FilterDefinition(
                    "f_CurrentTenant",
                    "TenantId = :f_tenantId",
                    new Dictionary<string, IType> { { "f_tenantId", NHibernateUtil.Guid } },
                    **false**);
                config.AddFilterDefinition(filterDef);

创建过滤器时,您可以设置最后一个参数“useManyToOne”= false。这将告诉 NH 不要在连接上使用过滤器。

到目前为止看来它已经奏效了:D

I'm looking for this exact problem with nullable optional fields

Note your OR will avoid using indexes so it may be slower.

UPDATE: I think I found the fix:

                // register filters
                var filterDef = new FilterDefinition(
                    "f_CurrentTenant",
                    "TenantId = :f_tenantId",
                    new Dictionary<string, IType> { { "f_tenantId", NHibernateUtil.Guid } },
                    **false**);
                config.AddFilterDefinition(filterDef);

When you create the filter, you can set the last parameter called "useManyToOne" = false. This will tell NH to not use the filter on the joins.

So far it seems it has worked :D

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