I have to build a condition as below in the code. The number of conditions added as a mix of AND/OR may vary with the same parameters.
WHERE ( (name=? AND number=?) ) OR ( (name=? AND number=?) ) OR (...) OR (...)
I tried below code in a loop -- not working as expected:
someList.forEach(item -> {
SimpleExpression nameExp = Restrictions.eq("name", item.getName());
SimpleExpression numberExp = Restrictions.eq("number", item.getNumber());
Criterion criterion = Restrictions.and(nameExp, numberExp);
criteria.add(Restrictions.or(criterion));
}
With the code above, I am getting the output with AND in between the first condition and the second. I want OR between conditions 1 and 2.
WHERE ( (name=? AND number=?) ) AND ( (name=? AND number=?) )
How do I build the condition as mentioned at the top using the criteria?
You collect the criterions to a list and finally invoke criteria.add(Restrictions.or(list))
.