combining 2 or more properties in Hibernate Restrictions criteria query
I want to perform search action on entered string. whatever user enter it should search on first name and last name column.
//pseudo hql query
Select E.someproperty .... from Entity E where upper(E.firstName || '' || E.lastName) like upper(+userEntered+'%') //
In above code I combined two column and did search on it. I want similar functionality using hibernate criteria. I have tried following query but it didn`t work
empCriteria.add(Restrictions.disjunction().add(Restrictions.ilike("firstName",
userEntered, MatchMode.START)).add(Restrictions.ilike("lastName", userEntered,
MatchMode.START)));
I know it is just or condition..please let me know is there way to combine two column and do search on using criteria query
You can't do this with Restrictions
.
I'd strongly recommend doing it in HQL rather than with an SQL restriction in a Criteria
instance. You're in danger of breaking the portability if you start using SQL: the point of Hibernate is to abstract that layer away.
See this SO question for more details.
Its not possible to do like that. Since You need to merge two columns. So Please try in sqlRestrinctions.
There is a way, for example add in your mapping hbm.xml file this( || means concat in oracle):
<property name="completeName"
formula="FST_NAME || ' ' || LAST_NAME"/>
simply add in java bean, the new proprieties:
private String completeName;
public String getCompleteName() {
return completeName;
}
public void setCompleteName(String completeName) {
this.completeName = completeName;
}
After finally you can get this concatenation in your restrinction:
Criteria.add(Restrictions.like("completeName","%"+userEntered+"%").ignoreCase();
ps: if you are mapping directly on java bean us the formula in this way:
@Formula(value="FST_NAME || ' ' || LAST_NAME")
private String completeName;
链接地址: http://www.djcxy.com/p/37100.html