在Hibernate限制条件查询中组合2个或更多属性 [英] combining 2 or more properties in Hibernate Restrictions criteria query

查看:75
本文介绍了在Hibernate限制条件查询中组合2个或更多属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对输入的字符串执行搜索操作。

  //假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中执行它,而不要在 Criteria 实例中使用SQL限制。如果您开始使用SQL,那么您有可能破坏可移植性:Hibernate的要点是将该层抽象出去。



请参阅这是SO问题了解更多详情。


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.

这篇关于在Hibernate限制条件查询中组合2个或更多属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆