Loading published curriculum…

14 · Hibernate and Spring Data JPA in depth / LESSON 69 OF 102

JPQL, native queries, projections, and specifications

Choose the smallest query shape that expresses the use case clearly.

Before this lesson: Lazy loading, N+1 queries, and fetch plans

Understand the topic

JPQL addresses entities and their attributes; native SQL addresses database tables and columns. Bind parameters in both. Interface or constructor projections can return only the columns a screen needs instead of loading mutable entity graphs.

Derived method names are useful for simple predicates. Complex combinations may be clearer as explicit queries or Specifications using Criteria predicates. Bulk update/delete queries bypass ordinary managed-entity tracking, so clear or refresh stale context state intentionally.

EXAMPLE · JAVA

Repository projection fragment

interface TaskSummary { Long getId(); String getTitle(); }

@org.springframework.data.jpa.repository.Query(
 "select t.id as id, t.title as title from Task t where t.done = :done")
java.util.List<TaskSummary> summaries(
 @org.springframework.data.repository.query.Param("done") boolean done);

What to expect

Place the method in the Task repository and the projection in its accessible scope. It selects only ID and title for the requested state.

YOUR TURN

Put it into practice

  1. 01Build an owner-scoped read projection with optional title and status filters.
  2. 02Compare a derived query with a Specification and inspect generated SQL.
  3. 03Verify: All filters use bound values and every result remains scoped to the authenticated owner.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

Choose the smallest query shape that expresses the use case clearly.

  1. 01JPQL addresses entities and their attributes; native SQL addresses database tables and columns. Bind parameters in both. Interface or constructor projections can return only the columns a screen needs instead of loading mutable entity graphs.
  2. 02Derived method names are useful for simple predicates. Complex combinations may be clearer as explicit queries or Specifications using Criteria predicates. Bulk update/delete queries bypass ordinary managed-entity tracking, so clear or refresh stale context state intentionally.
  3. 03Trace the example: Place the method in the Task repository and the projection in its accessible scope. It selects only ID and title for the requested state.

A mistake worth avoiding

A bulk update succeeds but a previously loaded entity shows old values.

DEBUGGING ROOM

Something broke. Where do you look?

A bulk update succeeds but a previously loaded entity shows old values.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Does JPQL normally name database tables?

Choose one answer

BUILD IT YOURSELF

Build an owner-scoped read projection with optional title and status filters.

  1. 1.Build an owner-scoped read projection with optional title and status filters.
  2. 2.Compare a derived query with a Specification and inspect generated SQL.
  3. 3.Reproduce this failure, then repair it: A bulk update succeeds but a previously loaded entity shows old values.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Does JPQL normally name database tables?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

All filters use bound values and every result remains scoped to the authenticated owner.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references