[英]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
. 您不能通过
Restrictions
来做到这一点。
I'd strongly recommend doing it in HQL rather than with an SQL restriction in a Criteria
instance. 我强烈建议您在HQL中这样做,而不要在
Criteria
实例中使用SQL限制。 You're in danger of breaking the portability if you start using SQL: the point of Hibernate is to abstract that layer away. 如果您开始使用SQL,就有可能破坏可移植性:Hibernate的目的是将这一层抽象掉。
See this SO question for more details. 有关更多详细信息,请参见此SO问题 。
您可以将Resrtictions.sqlRestriction()
与SQL子句一起使用。
Its not possible to do like that. 不可能那样做。 Since You need to merge two columns.
由于您需要合并两列。 So Please try in sqlRestrinctions.
因此,请尝试使用sqlRestrinctions。
There is a way, for example add in your mapping hbm.xml file this( || means concat in oracle): 有一种方法,例如在映射hbm.xml文件中添加this(||在oracle中表示concat):
<property name="completeName"
formula="FST_NAME || ' ' || LAST_NAME"/>
simply add in java bean, the new proprieties: 只需在Java bean中添加新属性:
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: ps:如果您直接在java bean上映射,则公式将以这种方式:
@Formula(value="FST_NAME || ' ' || LAST_NAME")
private String completeName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.