简体   繁体   English

我无法使JPA CriteriaBuilder equal()谓词区分大小写

[英]I can not get JPA CriteriaBuilder equal() predicate to be case sensitive

I am building a REST-Interface for some Application and I use JPA and Hibernate to access a SQL-Database. 我正在为某些应用程序构建REST接口,并且使用JPA和Hibernate访问SQL数据库。

Now I am trying to check if a given nickname already exists. 现在,我正在尝试检查给定的昵称是否已经存在。

I am doing it this way: 我这样做是这样的:

CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<DBUser> query = builder.createQuery(DBUser.class);
Root<DBUser> from = query.from(DBUser.class);
Predicate uniqueNickname = builder.equal(from.get(DBUser_.nickName),newNickname);
query.select(from).where(uniqueNickname);
if (this.entityManager.createQuery(query).getResultList().size() == 0) {
   //ok 
} else {
   //not ok
}

If I now have a User TEst in the database and newNickname="teST" it puts the User TEst into the resultlist. 如果我现在在数据库中有一个用户TEst,并且有newNickname =“ teST”,它将把用户TEst放入结果列表。 (I checked what the name in the resultlist is and it says TEst) I am using the mysql connector "mysql-connector-java-6.0.6.jar". (我检查了结果列表中的名称是什么,并显示了TEst)。我正在使用mysql连接器“ mysql-connector-java-6.0.6.jar”。

Normally, CriteriaBuilder#equal is case-sensitive, you can achieve equals ignore case by comparing strings in uppercase variant: 通常, CriteriaBuilder#equal区分大小写,您可以通过比较大写变体形式的字符串来实现等于忽略大小写:

Predicate uniqueNickname = builder.equal(builder.upper(from.<String>get(DBUser_.nickName)),
                                         newNickname.toUpperCase());

In your case, the DB ignore the case sensitive completely. 在您的情况下,数据库将完全忽略大小写。

Checking your MySQL DB and change the collation setting (the collation with _ci suffix is case-insensitve) 检查MySQL数据库并更改排序规则设置(带有_ci后缀的排序_ci不区分大小写)

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

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