Spring Data JPA: @Query when the method name gets silly

Past three conditions a derived name is unreadable. Write the JPQL and name the method for what it means.

Code
interface OrderRepository extends JpaRepository<Order, Long> {

    @Query("""
           select o from Order o
           join fetch o.items
           where o.status = :status and o.placedAt > :since
           """)
    List<Order> recentByStatus(@Param("status") Status status,
                              @Param("since") Instant since);

    @Query(value = "select * from orders where json_extract(meta,'$.vip') = true",
           nativeQuery = true)
    List<Order> vipOrders();
}
Output
The join fetch loads items in the same query,
which is how you avoid N+1 on the list endpoint.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11