How to code variable number of 'Or' clauses with Criteria ?
Hi
I have the following criteria query as :
Code:
Criteria criteria = session.createCriteria(Parts.class);
Iterator<String> iterator = searchList.iterator();
Disjunction or = Restrictions.disjunction();
while (iterator.hasNext()){
or.add(Restrictions.like("partName", "%"+iterator.next()+"%"));
}
criteria.add(or);
criteria.add(Restrictions.in("partType", partType));
criteria.addOrder(Order.asc("partDesc"));
which produces the following where clause :
Code:
from PartsDb..Parts this_ where (this_.PartName like ? or this_.PartName like ? or this_.PartName like ?) and this_.PartType in (?, ?) order by this_.PartDesc asc
The entire 'OR' clause is in the braces which does not give the correct results. I want the query to be something like this (without the braces around the 'OR' clause):
Code:
from PartsDb..Parts this_ where this_.PartName like ? or this_.PartName like ? and this_.PartType in (?, ?) order by this_.PartDesc asc
So I am trying to write this :
Code:
Select * from dbo.Parts
where
PartName like '%ABC%'
or PartName like '%XYZ%'
or PartName like '%PQR%'
....
...
and PartType in ('a','b')
...
The number of objects in the 'searchList' will vary depending on what the user has entered. Any help on how to get the variable number of 'OR' clauses out of the braces will be highly appreciated !!
Thanks