Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 27

    Spring Data JPA

    Spring Data JPA turns repository interfaces into runtime implementations.

    Course progress0%
    Focus
    3 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Spring Data JPA turns repository interfaces into runtime implementations. You declare what you need; Spring generates the SQL. JpaRepository<Entity, Id> ships with 15+ methods for free.

    Informative example

    ts
    public interface OrderRepository extends JpaRepository<Order, Long> {
    // Derived query — Spring builds the SQL from the method name
    List<Order> findByUserIdAndStatusOrderByCreatedAtDesc(Long userId, OrderStatus status);
    // Pagination
    Page<Order> findByStatus(OrderStatus status, Pageable pageable);
    // Projection (only load what you need)
    @Query("select new com.acme.OrderSummary(o.id, o.status, o.totalCents) from Order o where o.userId = :uid")
    List<OrderSummary> summariesFor(@Param("uid") Long uid);
    // Aggregate
    @Query("select count(o) from Order o where o.status = ?1")
    long countByStatus(OrderStatus status);
    }

    Best practices

    • Method names work great for ≤3 conditions; beyond that use @Query JPQL.
    • Always paginate list endpoints — never findAll() in prod.
    • Use projections for read-heavy endpoints — load 3 columns, not 30.
    Ready to mark this lesson complete?Track your journey across the entire course.