0

I am testing Spring-Data-JPA with Spring-Boot, using hibernate-core 5.4.2.Final version.

For example, I have a Users table containing address and other columns.

My repository interface for this table is as below.

public interface UsersRepository extends JpaRepository<Users, Integer> {

    List<Users> findByAddressContaining(String keyword)
}

To my knowledge, if I name the method like I did, the query below will be executed.

SELECT * FROM Users u WHERE address LIKE '%keyword%';

The problem is, this partly works.

For example, there are two datas in users table, each datas having address of "abc" and "cde".

If i test the findByAddressContaing("abc"), the result size of List<> is 2. Right after if I run findByAddressContaining("de"), the result should be 1, but it is 2. If i run the same method findByAddressContaining("de") again, then the result becomes 1.

I can't find the way to solve this problem. Any suggestions would be helpful. :)


THE PROBLEM WAS NOT WITH THE Spring-Data-JPA, it was with Request Parameter.

This is my controller method.

@GetMapping("/v2/warehouses")
public void getAllWarehouses(@RequestParam(name = "address") String address,
                             @RequestParam(name = "limit") Integer limit,
                             @RequestParam(name = "offset") Integer offset,
                             HttpServletResponse response) {
        
    System.err.println("Request Param Address : " + address");

If I send request to the correct path using Postman, sometimes address comes as blank.("")

Does this happen normally??

Roy Ra
  • 504
  • 1
  • 6
  • 23
  • Please show a complete code example, including inserting the data and querying the method and an initial query demonstrating that the database is empty to start with. – Jens Schauder Nov 12 '20 at 06:28
  • Try to print the translated SQL query so you can check. Add these to your properties file: `logging.level.org.hibernate.SQL=DEBUG` and `logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE` – goldthelocks Nov 12 '20 at 06:52
  • @JensSchauder Complete code is quite complex, so I made a sample code so I can put it simply. – Roy Ra Nov 12 '20 at 11:58

1 Answers1

0

So the basic problem was not with the @RequestParam annotation, it was with the application.properties file.

After I added server.tomcat.uri-encoding=UTF-8 to the properties file, the bug didn't happen anymore.

Thanks!

Roy Ra
  • 504
  • 1
  • 6
  • 23