%Like% Query in spring JpaRepository

JavaJpaSpring Data-Jpa

Java Problem Overview


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 extendsJpaRepository<Registration,Long> {
  
	@Query("Select c from Registration c where c.place like :place")
	 List<Registration> findByPlaceContaining(@Param("place")String place);
}

Java Solutions


Solution 1 - Java

The spring data JPA query needs the "%" chars as well as a space char following like in your query, as in

@Query("Select c from Registration c where c.place like %:place%").

Cf. http://docs.spring.io/spring-data/jpa/docs/current/reference/html.

You may want to get rid of the @Queryannotation alltogether, as it seems to resemble the standard query (automatically implemented by the spring data proxies); i.e. using the single line

List<Registration> findByPlaceContaining(String place);

is sufficient.

Solution 2 - Java

You dont actually need the @Query annotation at all.

You can just use the following

    @Repository("registerUserRepository")
    public interface RegisterUserRepository extends JpaRepository<Registration,Long>{
    
    List<Registration> findByPlaceIgnoreCaseContaining(String place);

    }

Solution 3 - Java

For your case, you can directly use JPA methods. That code is like bellow :

Containing: select ... like %:place%

List<Registration> findByPlaceContainingIgnoreCase(String place);

here, IgnoreCase will help you to search item with ignoring the case.

Using @Query in JPQL :

@Query("Select registration from Registration registration where 
registration.place LIKE  %?1%")
List<Registration> findByPlaceContainingIgnoreCase(String place);

Here are some related methods:

  1. Like findByPlaceLike

    … where x.place like ?1

  2. StartingWith findByPlaceStartingWith

    … where x.place like ?1 (parameter bound with appended %)

  3. EndingWith findByPlaceEndingWith

    … where x.place like ?1 (parameter bound with prepended %)

  4. 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 :)

Solution 4 - Java

You can also implement the like queries using Spring Data JPA supported keyword "Containing".

List<Registration> findByPlaceContaining(String place);

Solution 5 - Java

Try this.

@Query("Select c from Registration c where c.place like '%'||:place||'%'")

Solution 6 - Java

You can have one alternative of using placeholders as:

@Query("Select c from Registration c where c.place LIKE  %?1%")
List<Registration> findPlaceContainingKeywordAnywhere(String place);

Solution 7 - Java

when call funtion, I use: findByPlaceContaining("%" + place);

or: findByPlaceContaining(place + "%");

or: findByPlaceContaining("%" + place + "%");

Solution 8 - Java

I use this:

@Query("Select c from Registration c where lower(c.place) like lower(concat('%', concat(:place, '%')))")

lower() is like toLowerCase in String, so the result isn't case sensitive.

Solution 9 - Java

answer exactly will be

-->` @Query("select u from Category u where u.categoryName like %:input%")
List findAllByInput(@Param("input") String input);

Solution 10 - Java

We can use native query

@Query(nativeQuery = true, value ="Select * from Registration as c where c.place like %:place%")

List<Registration> findByPlaceContaining(@Param("place")String place);

Solution 11 - Java

Found solution without @Query (actually I tried which one which is "accepted". However, it didn't work).

Have to return Page<Entity> instead of List<Entity>:

public interface EmployeeRepository 
                          extends PagingAndSortingRepository<Employee, Integer> {
    Page<Employee> findAllByNameIgnoreCaseStartsWith(String name, Pageable pageable);
}

IgnoreCase part was critical for achieving this!

Solution 12 - Java

You can just simply say 'Like' keyword after parameters..

List<Employee> findAllByNameLike(String name);

Solution 13 - Java

Us like this

@Query("from CasFhgDeviceView where deviceGroupName like concat(concat('%CSGW%', :usid), '%') ")

Solution 14 - Java

There can be various approaches. As mentioned in answer of many, if possible you can use JPA predefined template query.

List<Registration> findByPlaceContainingIgnoreCase(String place);

Also, you can append '%' in java layer before calling the above method.

If complex query, then you can normally use @Query one

@Query("Select r from Registration r where r.place like '%' || :place || '%'")

For readability, you can use below one

@Query("Select r from Registration r where r.place like CONCAT('%', :place, '%'")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsudeep cvView Question on Stackoverflow
Solution 1 - JavaHilleView Answer on Stackoverflow
Solution 2 - JavaAlexius DIAKOGIANNISView Answer on Stackoverflow
Solution 3 - JavaMd. Sajedul KarimView Answer on Stackoverflow
Solution 4 - JavaamanzoorView Answer on Stackoverflow
Solution 5 - JavaAshuView Answer on Stackoverflow
Solution 6 - JavaNitin PawarView Answer on Stackoverflow
Solution 7 - Javauser9290654View Answer on Stackoverflow
Solution 8 - JavaMateusz NiedbalView Answer on Stackoverflow
Solution 9 - JavaSulaymon HursanovView Answer on Stackoverflow
Solution 10 - JavaAasim aliView Answer on Stackoverflow
Solution 11 - Javacatch23View Answer on Stackoverflow
Solution 12 - JavaNamesh SilvaView Answer on Stackoverflow
Solution 13 - Javauser1355816View Answer on Stackoverflow
Solution 14 - JavaSatish PatroView Answer on Stackoverflow