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);
}
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 @Query
annotation alltogether, as it seems to resemble the standard query (automatically implemented by the spring data proxies); ie using the single line
List<Registration> findByPlaceContaining(String place);
is sufficient.
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);
}
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:
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 :)
您还可以使用 Spring Data JPA 支持的关键字"Containing"来实现类似的查询。
List<Registration> findByPlaceContaining(String place);
尝试这个。
@Query("Select c from Registration c where c.place like '%'||:place||'%'")
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);
when call funtion, I use: findByPlaceContaining("%" + place);
or: findByPlaceContaining(place + "%");
or: findByPlaceContaining("%" + place + "%");
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.
answer exactly will be
-->` @Query("select u from Category u where u.categoryName like %:input%") List findAllByInput(@Param("input") String input);
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);
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!
您可以在参数后简单地说“Like”关键字..
List<Employee> findAllByNameLike(String name);
Us like this
@Query("from CasFhgDeviceView where deviceGroupName like concat(concat('%CSGW%', :usid), '%') ")
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, '%'")
我不得不使用这样的东西CONCAT('%',:setName,'%')
This is now possible with Spring Data JPA . Check out http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example
Registration registration = new Registration();
registration.setPlace("UK");
ExampleMatcher matcher = ExampleMatcher.matchingAll()
.withIgnoreCase()
.withStringMatcher(StringMatcher.CONTAINING);
Example<Registration> example = Example.of(registration, matcher);
List<Registration> registrationList = registerUserRepository.findAll(example);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.