spring 用于不区分大小写搜索的类似 HQL 的运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13266229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:30:33  来源:igfitidea点击:

HQL like operator for case insensitive search

springhibernatespring-mvcsql-like

提问by madhu

I am implementing an autocomplete functionality using Jquery, when I type the name, it fetches the record from the db, The records stored in db are mixture of capital & small letters. I have written a HQL Query which fetches me the records with case-sensitive, but I need to records irrespective of case. Here is the query,

我正在使用 Jquery 实现自动完成功能,当我输入名称时,它从数据库中获取记录,存储在数据库中的记录是大写和小写字母的混合。我编写了一个 HQL 查询,它以区分大小写的方式获取记录,但我需要不分大小写地记录。这是查询,

List<OrganizationTB> resultList = null;
Query query = session.createQuery("from DataOrganization dataOrg where dataOrg.poolName   
like '%"+ poolName +"%'");
resultList =  query.list();    

Ex : If I have pool names, HRMS Data set, Hrms Data, Hr data etc... if I type HR or hr I need to get all the 3 records, which I'm not able to.

例如:如果我有池名称、HRMS 数据集、Hrms 数据、Hr 数据等......如果我输入 HR 或 hr,我需要获取所有 3 条记录,但我无法做到。

Please help...

请帮忙...

回答by Jigar Parekh

change your query to

将您的查询更改为

"from DataOrganization dataOrg where lower(dataOrg.poolName)   
like lower('%"+ poolName +"%')"

for more information have a look 14.3 doc

有关更多信息,请查看 14.3文档

回答by Vasile Bors

A good solution is:

一个好的解决办法是:

List<OrganizationTB> resultList = null;
Query query = session.createQuery("from DataOrganization dataOrg where lower(dataOrg.poolName) like :poolName");
query.setParameter("poolName", '%'+poolName.toLowerCase()+'%');
resultList =  query.list();

So you protect your code from SQL injection

所以你保护你的代码免受 SQL 注入