Restrictions.Disjunction().Add(Subqueries.WhereValue(1)

909 Views Asked by At

What does this mean in nhibernate. Can anyone give me a small example?Can someone give me an example of

Restrictions.Disjunction().Add(Subqueries.WhereValue(1).Eq(subquerycount)..

What do these @p3, @p7 do?

select  a1, a2 from table1
inner join table2
where (
@p3 = (select count(somevariable)
from t1)
or
table2.isspecific = 0
and @p7=(Select count...)
1

There are 1 best solutions below

0
On

I'm assuming subquerycount is a detached Criteria or QueryOver. With that in mind:

Restrictions.Disjunction()
  • The Restrictions class is a helper class for building... well, restrictions. These are translated by NHibernate into the WHERE portion of the SQL query that's ultimately generated.
  • The Disjunction method is a helper method that creates a disjunction. In this context, that's an arbitrary number of conditions joined together with OR. There's also Restrictions.Conjunction() which generates a conjunction, which is the same thing except joined together with AND.

This is what's creating the SQL in the WHERE clause that you posted. Specifically the OR part:

(
@p3 = (select count(somevariable)
from t1)
or -- Right here
table2.isspecific = 0
and @p7=(Select count...)
)

The next portion:

.Add(Subqueries.WhereValue(1).Eq(subquerycount)

We're saying "Take this disjunction and add a condition to it". We're basically adding an expression that will be OR'd with other expressions (if any).

The expression we're adding (Subqueries.WhereValue(1).Eq(subquerycount)) is (probably) equal to this bit in the SQL:

@p3 = (select count(somevariable)
from t1)

Hopefully that helps explain what the line in question was doing.

What do these @p3, @p7 do?

NHibernate generates parameterized SQL. When you supply a value (like "1" in your example), that gets sent to the database in the form of a parameter. The @p0 ... @pn values you see in the generated SQL are just parameters that are auto-generated by NHibernate for you.

In your example, I'm guessing @p3 gets assigned to 1.