I would like to write a like query in JpaRepository
but it is not returning anything :
LIKE '%place%'
-its not working.
LIKE 'place'
works perfectly.
Here is my code :
@Repository("registerUserRepository")
public interface RegisterUserRepository extends
JpaRepository<Registration,Long>{
@Query("Select c from Registration c where c.place like:place")
List<Registration> findByPlaceContaining(@Param("place")String place);
}
when call funtion, I use:
findByPlaceContaining("%" + place);
or:
findByPlaceContaining(place + "%");
or:
findByPlaceContaining("%" + place + "%");
answer exactly will be
You can have one alternative of using placeholders as:
You can also implement the like queries using Spring Data JPA supported keyword "Containing".
For your case, you can directly use JPA methods. That is like bellow:
Containing: select ... like %:place%
here, IgnoreCase will help you to search item with ignoring the case.
Using @Query in JPQL:
Here are some related methods:
Like
findByPlaceLike
… where x.place like ?1
StartingWith
findByPlaceStartingWith
… where x.place like ?1 (parameter bound with appended %)
EndingWith
findByPlaceEndingWith
… where x.place like ?1 (parameter bound with prepended %)
Containing
findByPlaceContaining
… where x.place like ?1 (parameter bound wrapped in %)
More info , view this link , this link and this
Hope this will help you :)
You dont actually need the
@Query
annotation at all.You can just use the following