Spring Data JPA introduces query parser!
The Problem
One of Spring Data JPA’s handy features is letting you plugin in custom JPA queries through its @Query
annotation.
This allows some flexiblity because you are still able to offer sort parameters to the consumers of your app. Check out the example below:
interface SampleRepository extends CrudRepository<Employee, Long> {
@Query("select e from Employee e where e.firstName = :firstName")
List<Employee> findCustomEmployees(String firstName, Sort sort);
}
Spring Data JPA will turn this custom query into a JPA query when provided not just with a criteria (firstName
) but also a custom sort via findCustomEmployees("Alice", Sort.by("lastName"))
…