Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate restrictions and/or order

small questions about Restrictions.or and Restrictions.and

If I do something like this:

...
criterion = criterionA;
criterion = Restrictions.and(criterion, criterionB);
criterion = Restrictions.or(criterion, criterionC);
criterion = Restrictions.and(criterion, criterionD);

Will this be treated as:

(A and B) or (C and D) (following mathematical conventions)

Or will it be treated in the order it the restrictions have been added:

(((A and B) or C) and D)

Please also add references if there are any...

like image 858
Fortega Avatar asked Jun 26 '09 09:06

Fortega


People also ask

What is restriction in Hibernate criteria?

The Criteria interface makes it easy to selectively fetch the data on the basis of conditions in the select query. The Restriction class in hibernate provide several methods that can be used as conditions (also known as Criterion). These conditions are added to a criteria object with the add() method.

How do you put restrictions in criteria query?

Restrictions with CriteriaCriteria cr = session. createCriteria(Employee. class); Criterion salary = Restrictions.gt("salary", 2000); Criterion name = Restrictions. ilike("firstNname","zara%"); // To get records matching with OR conditions LogicalExpression orExp = Restrictions.or(salary, name); cr.


1 Answers

It should be treated as the latter

(((A and B) or C) and D)

You could do

criterion = Restriction.or(Restrictions.and(criterionA, criterionB), Restrictions.and(criterionC, criterionD))

If you want the first solution

like image 134
jitter Avatar answered Sep 25 '22 02:09

jitter