hibernate restrictions and/or order
Question
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...
Solution
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
OTHER TIPS
there are no precedence rules (like in a programming language or in a CFG parser), method calls order unambiguously determines the expression.
(A and B) or (C and D) must be translated to:
import static org.hibernate.criterion.Restrictions.*;
...
criterion = or(and(A, B), and(C,D));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow