Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.
Some time case arises, where we need a custom query to fulfil one test case. We can use @Query annotation to specify a query within a repository. Following is an example.
Spring Data JPA provides CRUD API, so you don't have to write boilerplate code. You just need to create a repository interface and spring will provide implementation automatically.
We can create a SQL query with the @Query annotation by following these steps: Add a query method to our repository interface. Annotate the query method with the @Query annotation, and specify the invoked query by setting it as the value of the @Query annotation's value attribute.
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); i.e. 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 :)
You can also implement the like queries using Spring Data JPA supported keyword "Containing".
List<Registration> findByPlaceContaining(String place);
Try this.
@Query("Select c from Registration c where c.place like '%'||:place||'%'")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With