使用 NHibernate ICriteria/QueryOver 查询向外连接添加条件

发布于 2024-10-26 01:39:41 字数 422 浏览 1 评论 0原文

使用 QueryOver 或 ICriteria 查询时,有没有办法在 NHibernate 中的外连接上指定附加条件?

我需要在外连接表上添加一些额外条件,但 NHibernate 总是将它们添加到末尾的 WHERE 子句中 - 这不会获得正确的行为(请参阅 http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria- on-outer-joined-tables.aspx)。

我似乎找不到任何方法来使用 Criteria 或 QueryOver 语法来执行此操作...

谢谢

Is there a way to specify additional conditions on outer joins in NHibernate when querying using QueryOver or ICriteria?

I need some extra conditions on the outer join-ed table, but NHibernate always adds them to the WHERE clause at the end - which does not get the correct behaviour (see http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria-on-outer-joined-tables.aspx).

I can't seem to find any way to do this using Criteria or the QueryOver syntax...

Thanks

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

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

发布评论

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

评论(3

猫弦 2024-11-02 01:39:41

你可能很久以前就明白了这一点。解决方案是在 JoinAlias 方法中添加 ICriteria 参数,如下所示:

Party aliasParty = null;
Party aliasPartyFrom = null;
var parties = QueryOver.Of<Party>(() => aliasParty)
              .Left.JoinAlias(
                               () => aliasParty.AccountabilitiesFrom, 
                               () => aliasAccFrom, 
                               Restrictions.On(() => aliasAccFrom.TimeTo).IsNull)

我对 aliasAccFrom 有限制,我希望 TimeTo 为 null,在最后一行代码中。

You probably figure out this long time ago. Solution is to add ICriteria parameter in JoinAlias method, like this:

Party aliasParty = null;
Party aliasPartyFrom = null;
var parties = QueryOver.Of<Party>(() => aliasParty)
              .Left.JoinAlias(
                               () => aliasParty.AccountabilitiesFrom, 
                               () => aliasAccFrom, 
                               Restrictions.On(() => aliasAccFrom.TimeTo).IsNull)

I have restriction on aliasAccFrom, where i want that TimeTo is null, in last line of code.

傲鸠 2024-11-02 01:39:41

(回答了我自己的问题 - 抱歉!)

Fabio 在 NHibernate 列表上回答了类似的查询 - 只是想我将其发布在这里。

从 NH3.0 开始,这可以通过 Criteria 实现。
HQL中的功能
http://fabiomaulo.blogspot.com/2009/05 /nhibernate-210-hql-with-clause.html

使用 Criteria 看看
CreateAlias(字符串关联路径、字符串别名、JoinType joinType、ICriterion withClause)
CreateCriteria(string AssociationPath, string alias, JoinType joinType, ICriterion withClause)

使用 QueryOver 它不可用,但这里有一个 JIRA: https://nhibernate.jira.com/browse/NH-2592

(Answered my own question - sorry!)

Fabio answered a similar query on the NHibernate list - just thought I'd post it here.

That is possible with Criteria since NH3.0.
The feature in HQL
http://fabiomaulo.blogspot.com/2009/05/nhibernate-210-hql-with-clause.html

With Criteria have a look to
CreateAlias(string associationPath, string alias, JoinType joinType, ICriterion withClause)
CreateCriteria(string associationPath, string alias, JoinType joinType, ICriterion withClause)

With QueryOver it is not available but there's a JIRA for this here: https://nhibernate.jira.com/browse/NH-2592

烂人 2024-11-02 01:39:41

我尝试了以下查询并查询了

SystemUser systemUser= null;
SurveyRequests SurveyRequests = null;

var Query2 = Session.QueryOver<SystemUser>(() => systemUser)
               .Left.JoinAlias(() => systemUser.SurveyRequests, 
               () => surveyRequest,
               Restrictions.On(()=>surveyRequest.Survey.Id).IsIn(new object []{surveyID }))

i tried the following query with query over

SystemUser systemUser= null;
SurveyRequests SurveyRequests = null;

var Query2 = Session.QueryOver<SystemUser>(() => systemUser)
               .Left.JoinAlias(() => systemUser.SurveyRequests, 
               () => surveyRequest,
               Restrictions.On(()=>surveyRequest.Survey.Id).IsIn(new object []{surveyID }))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文