如何使用标准在休眠状态下进行不区分大小写的搜索? [英] how to do case insensitive search in hibernate using criteria?

查看:75
本文介绍了如何使用标准在休眠状态下进行不区分大小写的搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用休眠模式.我想进行不区分大小写的搜索.如下.

I am using hibernate. i want to do case insensitive search. as below.

criteria.add(Restrictions.eq("id", id).ignoreCase());

但是在以下情况下该如何做?

but how to do the same in below case?

criteria.add(Restrictions.in("name", names));//此处命名一个名称列表.

在不区分大小写的情况下如何搜索姓名?

here how to search names in case insensitive?

请帮助.

谢谢!

推荐答案

您必须自己编写一个新表达式(CaseInsensitiveInExpression).使用org.hibernate.criterion.InExpression的代码作为起点,并对其进行转换,以使生成的SQL子句类似于

You'll have to write a new expression (CaseInsensitiveInExpression) yourself. Use the code of org.hibernate.criterion.InExpression as a starting point, and transform it so that the generated SQL clause is something like

lower(theColumnName) in (?, ?, ?)

和传递给查询的值是通过toLowerCase()传递的原始值(如org.hibernate.criterion.ILikeExpression一样)

and the values passed to the query are the original values passed through toLowerCase() (like the org.hibernate.criterion.ILikeExpression does)

如果要使数据库不可知,请使用dialect.getLowercaseFunction()来获取数据库函数,该函数将字符串转换为小写字母,就像ILikeExpression一样.

If you want to make database-agnostic, use dialect.getLowercaseFunction() to get the database function which transforms a string to lower-case, as the ILikeExpression does.

这篇关于如何使用标准在休眠状态下进行不区分大小写的搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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