java Hibernate 标准中区分大小写的搜索

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

Case sensitive Search in Hibernate Criteria

javasql-serverhibernate

提问by RAS

I'm using Hibernate 3 with SQL Server 2008. I want to do case sensitive search in Hibernate Criteria. I'm not able to achieve it. My code is as below:

我在 SQL Server 2008 中使用 Hibernate 3。我想在 Hibernate Criteria 中进行区分大小写的搜索。我无法实现它。我的代码如下:

Criteria criteria = session.createCriteria(User.class); /*line 1*/
criteria.add(Restrictions.eq("userName", userName));    /*line 2*/

I have a class User.javawhich contains a String userName.

我有一个User.java类,它包含一个String userName.

DB Entries:

数据库条目:

id | user_name
--------------
1  | abc
2  | xyz

Now, if I pass "abc"as userNamein line 2, then it should return the first record from the db. But if I pass "Abc", "ABC", "aBC"etc. as userNamein line 2, no records should be fetched.

现在,如果我通过"abc"userName在第2行,那么就应该从数据库返回的第一条记录。但是,如果我通过"Abc""ABC""aBC"等如userName在第2行,没有记录将被取回。

I've visited this linkbut it's not helpful to me as I don't want to use collation with hql or with SQL Server. I'm open to use collation with Criteriabut don't know how to do it.

我访问过此链接,但它对我没有帮助,因为我不想将整理与 hql 或 SQL Server 一起使用。我愿意使用整理,Criteria但不知道如何去做。

回答by ez2sarang

Criteria criteria = s.createCriteria(User.class);
criteria.add(
    Expression.sql("userName = ? collate Latin1_General_CS_AS_KS_WS", userName, new StringType())
);

回答by alexey28

You can't with hibernate. Problem is in MS SQL - by default it use case insensitive collation. There can be two workarounds:

你不能用休眠。问题出在 MS SQL 中 - 默认情况下它使用不区分大小写的排序规则。可能有两种解决方法:

1 Select with hibernate and do program filtering of result

1 选择hibernate并对结果进行程序过滤

2 When create table use case sensitive collation for filed you nead to search: COLLATE SQL_Latin1_General_CP850_BIN2 or other specified for your server (read from SYSTEMPROPERTY).

2 创建表时使用区分大小写的字段排序规则,您需要搜索:COLLATE SQL_Latin1_General_CP850_BIN2 或其他为您的服务器指定的(从 SYSTEMPROPERTY 读取)。

回答by esmoreno

You can try this (using ilike in Restrictions API)

你可以试试这个(在 Restrictions API 中使用 ilike)

Criteria criteria = s.createCriteria(User.class);   
criteria.add(Restrictions.ilike("userName", userName));

Regards

问候

回答by Deniz

This may not be suitable for you but it may be better to use lucene/solr for these kind of queries. In addition to case insensitive queries it will also handle other common scenarios for you.

这可能不适合您,但最好将 lucene/solr 用于此类查询。除了不区分大小写的查询之外,它还将为您处理其他常见场景。

回答by Vespucci75fr

I add the same issue, and I changed the collation type in the database table straight in the mariadb server.

我添加了相同的问题,并直接在 mariadb 服务器中更改了数据库表中的排序规则类型。

I create the table field with "COLLATE latin1_general_cs"

我用“COLLATE latin1_general_cs”创建表字段

So every search on this field will be case sensitive.

因此,对该字段的每次搜索都将区分大小写。

My table field looks like this.

我的表字段看起来像这样。

'case_sensitive_key' varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL DEFAULT '',

'case_sensitive_key' varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL DEFAULT '',

Hope it will help some of you.

希望它会帮助你们中的一些人。