QuickDevGuide

Home › Spring Boot

JPA Queries, Paging & Relationships — Spring Boot

Query options Style Example Derived query findByStatusAndCreatedAtAfter(Status s, Instant t) JPQL @Query("select o from Order o where o.total > :min") Native SQL @Query(value = "...", nativeQuery = true) Projections interface or record with only needed fields Paging & sorting Page<T> findAll(Pageable p), PageRequest.of(0, 20, Sort.by("name")) Dynamic filters Specification<T> / Querydsl Bulk update @Modifying @Query("update ...") Relationships @ManyToOne — set fetch = FetchType.LAZY (default is EAGER). @OneToMany(mappedBy = "...") — lazy by default; avoid huge collections. Solve N+1 with @EntityGraph(attributePaths = "items") or join fetch. Turn off spring.jpa.open-in-view so lazy loading can't silently run queries in the web layer.

Key Takeaways