Java 不区分大小写的等于使用 Hibernate Criteria

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/624604/
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-08-11 17:05:39  来源:igfitidea点击:

Case-insensitive equals using Hibernate Criteria

javahibernate

提问by jon077

I've seen Restrictions.ilike('property', '%value%'), but would like to generate SQL like: lower(property) = 'value'. Any ideas?

我见过 Restrictions.ilike('property', '%value%'),但想生成如下 SQL:lower(property) = 'value'。有任何想法吗?

I used:

我用了:

Restrictions.eq("email", email).ignoreCase()

since Expression is deprecated. The SimpleExpression will call toLowerCase() on the value, so it is not necessary to do it beforehand.

因为表达式已被弃用。SimpleExpression 将对值调用 toLowerCase(),因此没有必要事先执行此操作。

See: SimpleExpression source

请参阅: SimpleExpression 源代码

采纳答案by John Wagenleitner

Be careful of using ilike because it would allow someone to enter things like "test%" and match. I use the following to do a case-insensitive equal in one app:

使用 ilike 时要小心,因为它会允许某人输入诸如“test%”之类的内容并进行匹配。我使用以下内容在一个应用程序中执行不区分大小写的相等操作:

...
Criteria crit=session.createCriteria(Event.class);
crit.add(Expression.eq("rsvpCode","test1").ignoreCase());
...

回答by Uri

I'm not absolutely sure, but when you use Restriction.eq you obtain a SimpleExpression object, and that object suppports an ignoreCase() operation which I've never tried using but sounds like it could make a difference.

我不确定,但是当您使用 Restriction.eq 时,您会获得一个 SimpleExpression 对象,并且该对象支持我从未尝试使用的 ignoreCase() 操作,但听起来它可能会有所作为。

Kudos to Hibernate for not documenting what this method actually does.

感谢 Hibernate 没有记录这个方法的实际作用。

回答by Andy A

Expression is now deprecated. Use Restrictions instead ...

表达式现已弃用。改用限制...

crit(Restrictions.eq("firstName", firstName).ignoreCase());

回答by Mohd Kose Avase

crit(Restrictions.eq("firstName", firstName).ignoreCase());works.

crit(Restrictions.eq("firstName", firstName).ignoreCase());作品。

This did returns a SimpleExpression Objectbut It generates lower(firstname)query. So this works.

这确实返回了SimpleExpression Object但它生成了lower(firstname)查询。所以这是有效的。

回答by Satyam Koyani

As Andy's answer suggests, this for case-insensitive searches but it is also works through to Hibernate version 4.1:

正如安迪的回答所暗示的那样,这适用于不区分大小写的搜索,但它也适用于 Hibernate 4.1版:

crit(Restrictions.eq("firstName", firstName).ignoreCase());

Versions 4.1.1 and laterof Hibernate do not support the ignoreCase()method on Restriction.eq(). For that, we have to use ilikewith MatchMode.

Hibernate 4.1.1 及更高版本不支持 上的ignoreCase()方法Restriction.eq()。为此,我们必须使用ilikewith MatchMode

Criteria crit = session.createCriteria(ENTITY.class);
crit.add(Restrictions.ilike('PROPERTY NAME', 'VALUE', MatchMode.ANYWHERE));

As an example, for a USERentity with id, name, surnameproperties, a case-insensitive search based on name will be:

例如,对于具有id、name、surname属性的USER实体,基于名称的不区分大小写的搜索将是:

Criteria crit = session.createCriteria(USER.class);
crit.add(Restrictions.ilike('name', 'Satyam', MatchMode.ANYWHERE));

This will return all results, case insensitive.

这将返回所有结果,不区分大小写。

回答by Shreya Sharma

If requirement is lower(property) = 'value'than best way is to use Restrictions.eq with ignoreCase() instead of using ilike cuz here the exact value is know. ilike is beneficial to search a pattern or when we are not aware of the exact value. Only problem with ignoreCase() is, it will not restrict your results to lower case and also include results with other cases , but i think is is the only available option hibernate offers.

如果要求lower(property) = 'value'不是最好的方法是使用 Restrictions.eq 和 ignoreCase() 而不是使用 ilike 因为这里的确切值是知道的。ilike 有助于搜索模式或当我们不知道确切值时。ignoreCase() 的唯一问题是,它不会将您的结果限制为小写,并且还包括其他情况的结果,但我认为这是 hibernate 提供的唯一可用选项。

 Criteria criteria = session.createCriteria(ClassName.class);
 criteria.add(Restrictions.eq("PROPERTY", propertyName).ignoreCase()).uniqueResult();

Now if I understand your question correctly, your db has these values: ABC, abc, aBC, aBc...and so on, you want to convert them to lower case and then compare with some value say "ABC" or "abc". YOu can do this by further processing the list you got.

现在,如果我正确理解您的问题,您的数据库有这些值:ABC、abc、aBC、aBc...等等,您想将它们转换为小写,然后与某个值进行比较,例如“ABC”或“abc” . 你可以通过进一步处理你得到的列表来做到这一点。

You can save this result in a list.

您可以将此结果保存在列表中。

List<ClassName> listOfAllMatches =  criteria.list(); // gives all matched results (ABC, abc, aBC, aBc)



List<ClassName> finalResult = listOfAllMatches.stream().map(x -> x.getPropertyName().toLowerCase()).filter(y ->y.getPropertyName()== value).collect(Collectors.toList());

//convert this field to lower case using map() and filter your results with filter() function.

YOU can further add this list to Criteria and process it.

您可以进一步将此列表添加到 Criteria 并进行处理。