Using custom JPA
If all else fails and we can’t seem to bend Spring Data’s query derivation tactics to meet our needs, it’s possible to write the JPQL ourselves.
In our repository interface, we can create a query method as follows:
@Query("select v from VideoEntity v where v.name = ?1") List<VideoEntity> findCustomerReport(String name);
The preceding method can be explained as follows:
@Query
is Spring Data JPA’s way to supply a custom JPQL statement.- It’s possible to include positional binding parameters using
?1
to tie it to thename
argument. - Since we are providing the JPQL, the name of the method no longer matters. This is our opportunity to pick a better name than what custom finders constrained us to.
- Because the return type is
List<VideoEntity>
, Spring Data will form a collection.
Using @Query
essentially sidesteps any query writing done by Spring Data and uses the user’s supplied...