简体   繁体   English

不区分大小写的等于使用 Hibernate 标准

[英]Case-insensitive equals using Hibernate Criteria

I've seen Restrictions.ilike('property', '%value%'), but would like to generate SQL like: lower(property) = 'value'.我见过 Restrictions.ilike('property', '%value%'),但想生成 SQL,例如:lower(property) = 'value'。 Any ideas?有任何想法吗?

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 源

Expression is now deprecated.现在不推荐使用表达式。 Use Restrictions instead...改为使用限制...

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

Be careful of using ilike because it would allow someone to enter things like "test%" and match.使用 ilike 时要小心,因为它会允许某人输入“test%”之类的内容并进行匹配。 I use the following to do a case-insensitive equal in one app:我使用以下内容在一个应用程序中执行不区分大小写的相等操作:

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

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 later of Hibernate do not support the ignoreCase() method on Restriction.eq() . Hibernate 的4.1.1 及更高版本不支持Restriction.eq()上的ignoreCase()方法。 For that, we have to use ilike with MatchMode .为此,我们必须将ilikeMatchMode一起使用。

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

As an example, for a USER entity with id, name, surname properties, a case-insensitive search based on name will be:例如,对于具有id、name、surname属性的USER实体,基于 name 的不区分大小写的搜索将是:

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

This will return all results, case insensitive.这将返回所有结果,不区分大小写。

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 object,并且 object 支持一个我从未尝试过使用的 ignoreCase() 操作,但听起来它可能会有所作为。

Kudos to Hibernate for not documenting what this method actually does.感谢 Hibernate 没有记录此方法的实际作用。

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.如果要求lower(property) = 'value' ,最好的方法是将 Restrictions.eq 与 ignoreCase() 一起使用,而不是使用 ilike cuz,这里的确切值是已知的。 ilike is beneficial to search a pattern or when we are not aware of the exact value. ilike 有利于搜索模式或当我们不知道确切值时。 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. 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".现在,如果我正确理解您的问题,您的数据库具有以下值:ABC、abc、aBC、aBc ......等等,您希望将它们转换为小写,然后与某个值进行比较,例如“ABC”或“abc” . YOu can do this by further processing the list you got.你可以通过进一步处理你得到的列表来做到这一点。

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 并对其进行处理。

You can use Criterion.ilike.您可以使用 Criterion.ilike。 For more information check this link .有关更多信息,请查看此链接

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

This did returns a SimpleExpression Object but It generates lower(firstname) query.这确实返回了一个SimpleExpression Object但它会生成lower(firstname)查询。 So this works.所以这行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM