开发者

Is it possible to search for same value on multiple columns with Hibernate Criteria?

开发者 https://www.devze.com 2023-01-08 15:51 出处:网络
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 wi

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));
0

精彩评论

暂无评论...
验证码 换一张
取 消