views:

34

answers:

2

I have search query that has many optional parameters and then search word field that searches many columns. Is it possible to use hibernate criteria for this purpose? I need to search many columns with same search word.

Example code (what is not working correctly)

if(isNotEmpty(searchWord))
{
    criteria.add(Restrictions.like("description", searchWord));
    criteria.add(Restrictions.like("name", searchWord));
}
+1  A: 

It looks like you actually need an OR:

Criterion description = Restrictions.like("description", searchWord);
Criterion name = Restrictions.like("name", searchWord);
LogicalExpression orExp = Restrictions.or(description, name);
criteria.add(orExp);
Pascal Thivent
+1 Thanx for this, but it only allows two values in that or, so I used sql restrictions.
newbie
A: 

I solved this with using custom sql restrictions, example here:

Object[] values = {  }; // values here
Type[] types = {  }; // value types here
criteria.add(Restrictions.sqlRestriction("sql query here", values, types));
newbie