在Hibernate限制条件查询中组合2个或更多属性 [英] combining 2 or more properties in Hibernate Restrictions criteria query
问题描述
我想对输入的字符串执行搜索操作。
//假hql查询
请选择E.一些属性......来自实体E,其中上(E.firstName ||''|| E.lastName)像上(+ userEntered +'%')//
在上面的代码中,我组合了两列,并对其进行搜索。我想要使用休眠条件的类似功能。
我尝试过下面的查询,但它没有工作
empCriteria.add(Restrictions.disjunction()。add (Restrictions.ilike(firstName,
userEntered,MatchMode.START)).add(Restrictions.ilike(lastName,userEntered,
MatchMode.START)));
我知道这只是或者情况。请让我知道是否有办法将两列和使用标准查询进行搜索
您无法使用 我强烈建议在HQL中执行它,而不要在 请参阅这是SO问题了解更多详情。 I want to perform search action on entered string. whatever user enter it should search on first name and last name column. 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 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 I'd strongly recommend doing it in HQL rather than with an SQL restriction in a See this SO question for more details. 这篇关于在Hibernate限制条件查询中组合2个或更多属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!限制。
Criteria
实例中使用SQL限制。如果您开始使用SQL,那么您有可能破坏可移植性:Hibernate的要点是将该层抽象出去。
//pseudo hql query
Select E.someproperty .... from Entity E where upper(E.firstName || '' || E.lastName) like upper(+userEntered+'%') //
empCriteria.add(Restrictions.disjunction().add(Restrictions.ilike("firstName",
userEntered, MatchMode.START)).add(Restrictions.ilike("lastName", userEntered,
MatchMode.START)));
Restrictions
.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.