Skip to content
Advertisement

Unexpected empty result using spring query method

I am building an application with a REST API using spring boot and JPA connected to a MySQL database. To search for names in the User class I have implemented a query method: List< User > findByFirstnameLike( String name );

This method only returns a result on an exact match, which is not what I wanted. Have I misunderstood something about its use?

The repository interface:

JavaScript

The service bean method:

JavaScript

The REST interface method:

JavaScript

Entity class:

JavaScript

So… having a user in the database with the name “Adam”, the query method returns an empty list for any string except for “Adam”.

Edit 2: After turning on show SQL and inserting an object to the database, I search for a part of the first name and yield this output and an empty List:

JavaScript

The searhing for an exact match which returns an array with one object:

JavaScript

Advertisement

Answer

Try to use findByFirstnameContaining instead of findByFirstnameLike.

I am not sure how like is interpreted. But the docs says:

JavaScript

And I suppose you want to search for %dam%.

If you want to bound the wildcard at the beginning or ending this is also possible: findByFirstnameStartingWith or findByFirstnameEndingWith.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement