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

    JPQL

    JPQL (Java Persistence Query Language) looks like SQL but operates on entities and fields, not tables and columns.

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

    Introduction

    JPQL (Java Persistence Query Language) looks like SQL but operates on entities and fields, not tables and columns. It's portable across DBs and integrates cleanly with Spring Data @Query.

    Informative example

    ts
    public interface OrderRepository extends JpaRepository<Order, Long> {
    @Query("""
    select o from Order o
    join fetch o.items i -- avoid N+1
    where o.userId = :userId
    and o.status in :statuses
    order by o.createdAt desc
    """)
    List<Order> findFor(@Param("userId") Long userId,
    @Param("statuses") Collection<OrderStatus> statuses);
    @Modifying
    @Query("update Order o set o.status = :status where o.id = :id")
    int updateStatus(@Param("id") Long id, @Param("status") OrderStatus status);
    }

    Best practices

    • Use join fetch to eliminate N+1 on collections you read together.
    • @Modifying + @Transactional for bulk updates/deletes.
    • JPQL > native SQL when portability matters; native SQL > JPQL when DB-specific features matter.
    Ready to mark this lesson complete?Track your journey across the entire course.