0

I'm trying to make a case sensitive query in a repository that extends JpaRepository, in a Spring Boot 2 project with Spring Data JPA.

It's supposed that if I don't use IgnoreCase, the query should be case sensitive, but it doesn't occur.

Snippet:

    log.info("Received name: " + name);
    Player player = playerRepository.findByName(name).orElseThrow(()-> new NonExistingPlayerNameException(name));
    log.info("Repository player name: " + player.getName());

Console result:

    2020-03-19 13:05:02.831  INFO 13739 [...].controllers.PlayerController   : Received name: peter  
    2020-03-19 13:05:02.884  INFO 13739 [...].controllers.PlayerController   : Repository player name: Peter

As you can see, the query is case insensitive, but I want it case sensitive.

Am I doing anything wrong? How can I make the query case sensitive?

As a workaround, I can make the check after I receive the object and act in consequence, but I expected the query to be case sensitive.

Thanks in advance,
Carlos.

Edit:
Thanks for the answers and sorry about the missed information.
I'm using a MySQL database (mysql Ver 8.0.19-0ubuntu0.19.10.3 for Linux on x86_64 ((Ubuntu))) in a Ubuntu Desktop 19.10.

I'm sorry but I don't know how to log the actual query that gets executed.

What I can see is that the database allows me to save a register with upper and lower case, I can have a row with field name 'Peter' and another one with name 'peter'. I mean, the database seems that doesn't change the data to store it.

I will take a look at the answer about MSSQL Server.

Thank you,
Carlos.

Carlos
  • 105
  • 2
  • 6
  • What is the persistence store you are using ? Ignoring case depends on the persistence store . [reference](https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation) – R.G Mar 19 '20 at 13:10
  • You may also log the actual query that gets executed – R.G Mar 19 '20 at 13:12
  • If the query is case sensitive depends on your database and even sometimes the combination of your datbase and operating system. Please clarify in your question which database you are using. – M. Deinum Mar 19 '20 at 13:46

2 Answers2

0

Your problem not its in Spring, your problem is your Database.

See this link for more information

In this thread they explain how to change your database, or rather how to change the fields that you want to be sensitive

Is the LIKE operator case-sensitive with MSSQL Server?

Adrian Lagartera
  • 442
  • 1
  • 3
  • 17
0

Thank you all for all of the information and "clues" to find out the answer.
The problem was the collation: I was using utf8mb4_0900_ai_ci (case insensitive). I changed it to utf8mb4_es_0900_as_cs and everything works fine.
Thank you!!!
Carlos.

Carlos
  • 105
  • 2
  • 6