%Like% Query in spring JpaRepository

前端 未结 10 1650
南方客
南方客 2020-12-04 11:36

I would like to write a like query in JpaRepository but it is not returning anything :

LIKE \'%place%\'-its not working.

LIKE

相关标签:
10条回答
  • 2020-12-04 11:37

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

    or: findByPlaceContaining(place + "%");

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

    0 讨论(0)
  • 2020-12-04 11:40

    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);
    
        }
    
    0 讨论(0)
  • 2020-12-04 11:46

    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.

    0 讨论(0)
  • 2020-12-04 11:46

    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!

    0 讨论(0)
  • 2020-12-04 11:47

    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.

    0 讨论(0)
  • 2020-12-04 11:49

    Try this.

    @Query("Select c from Registration c where c.place like '%'||:place||'%'")
    
    0 讨论(0)
提交回复
热议问题