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 correct开发者_开发知识库ly)
if(isNotEmpty(searchWord))
{
criteria.add(Restrictions.like("description", searchWord));
criteria.add(Restrictions.like("name", searchWord));
}
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);
by using Disjunction we have add any number of columns and finally add the Disjunction obj to criteria.
Disjunction disCriteria = Restrictions.disjunction();
disCriteria.add(Restrictions.eq("name", searchWord));
disCriteria.add(Restrictions.eq("description", searchWord));
criteria.add(disCriteria);
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));
精彩评论