All we need is an easy explanation of the problem, so here it is.
Here I want to retrieve Hotels by hotel name
, but it is only returning the Hotels which have first letter which I have included.
I have search Hotel name as "Sigiriya", but when I type "s" it’s resulting Sigiriya. But when I start typing more ("igiriya"), the results were gone.
/**
*
* @param searchkey
* @return - List of hotels
*/
public List<Hotel> searchHotelByName( String searchkey){
searchkey = searchkey.toLowerCase();
searchkey = "%" + searchkey + "%";
return hotelRepository.findAllByHotelNameContentLike( searchkey );
}
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
Method 1
I think the reason is even though you are converting the search key to lower case, your database is containing upper case letters. Therefore use ignore case search
.
/**
*
* @param searchkey
* @return - List of hotels
*/
public List<Hotel> searchHotelByName( String searchkey){
searchkey = searchkey.toLowerCase();
return hotelRepository.findAllByHotelNameIgnoreCaseContaining( searchkey );
}
Method 2
Use the contains
JPA keyword
public List<Hotel> searchHotelByName( String searchkey){
return hotelRepository.findAllByHotelName IgnoreCaseContaining( searchkey );
}
contains
takes care of %
etc.
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0