Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding conditions to outer joins with NHibernate ICriteria/QueryOver query

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

like image 927
James Crowley Avatar asked Nov 28 '25 09:11

James Crowley


2 Answers

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.

like image 175
vllado2 Avatar answered Nov 29 '25 22:11

vllado2


(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

like image 30
James Crowley Avatar answered Nov 29 '25 22:11

James Crowley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!