Card 03/ 06

ExampleDifficulty: Advanced1 min

When a Method Name Can't Say It

A method name can express a filter and a sort. It can't express a join across two entities — there's no vocabulary in the naming convention for it, so @Query writes the JPQL directly instead.

java
interface RideJpaRepository extends JpaRepository<RideEntity, Long> {

    @Query("select r from RideEntity r join DriverEntity d on d.id = r.id where d.name = ?1")
    List<RideEntity> findByDriverName(String name);
}
text
@Query join found 1 ride(s) for driver Alex, id=1
run in a container — a real join, against real rows

?1 refers to the method's first parameter positionally. @Query is JPQL — written against entities and their fields, not table and column names — which is what keeps it portable across the databases Hibernate supports underneath.