Skip to content
Ameen Abozaid
JNR-029

Spring Data JPA

  • 7 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Repositories
  • derived queries
  • JPQL
  • native queries

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Define repositories extending JpaRepository and explain the generated implementation.
  2. Write derived queries and recognize when a name becomes unreadable.
  3. Write JPQL and native queries with @Query and parameters.
  4. Return projections (interface or record) for read-only views and use Pageable.

Lessons

  1. 01Repository interfaces
  2. 02Derived queries
  3. 03JPQL with @Query
  4. 04Native queries
  5. 05Projections and paging
  6. 06Demo: migrating the catalog to Spring Data JPA
  7. 07Demo: projections for paged listings

Lab and exercises

Lab

JNR-029-LAB1 · Catalog on JPA

Outcomes: 1–4 · Time: ~3 h

  1. Move products and categories to Spring Data JPA repositories.
  2. Add findBySku (derived) and a paged search by name, category, and price range (@Query).
  3. Return a ProductListItem projection for the listing; map to the API response in the service.
  4. Keep all existing endpoints and JSON unchanged.
  5. Run the existing API tests plus a repository test for the search.

Acceptance: API contract unchanged; tests pass.

Exercises (assessment: implement three queries from requirements)

IDOutcomeTask
JNR-029-EX011Declare CustomerRepository and use findById, save, count; explain who implements it.
JNR-029-EX022Write derived queries: products by status, products with name containing text (ignore case).
JNR-029-EX032Rewrite a 60-character derived method as a @Query.
JNR-029-EX043Write a JPQL query with named parameters: orders of a customer between two dates.
JNR-029-EX053Write a native query counting products per category and explain why native was chosen.
JNR-029-EX064Return a record projection and a Page sorted by price descending.
Show solutions
  • EX01 interface CustomerRepository extends JpaRepository<Customer, Long> {}; Spring Data creates a proxy implementation at startup.
  • EX02 List<Product> findByStatus(ProductStatus s); and List<Product> findByNameContainingIgnoreCase(String text);
  • EX03 @Query("select p from Product p where p.category.id = :cid and p.price.amount <= :max order by p.name") List<Product> findAffordable(@Param("cid") Long cid, @Param("max") BigDecimal max);
  • EX04 select o from Order o where o.customer.id = :cid and o.createdAt between :from and :to.
  • EX05 select category_id, count(*) from product group by category_id with nativeQuery = true; chosen for a reporting aggregate returned as a projection, not because it is the default.
  • EX06 Page<ProductListItem> list(Pageable p); called with PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "price.amount")) (check the property path against your mapping).
  • Assessment covers EX02, EX04, and EX06 (three queries from requirements).

Quiz

Outcomes: O1 repositories · O2 derived queries · O3 JPQL and native queries · O4 projections and paging.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Who creates the implementation of a JpaRepository interface?O1
  2. 2findById returns:O1
  3. 3findByNameContainingIgnoreCase(String) produces:O2
  4. 4A derived name that is 60 characters long should be:O2
  5. 5JPQL queries refer to:O3
  6. 6Query input must be passed:O3
  7. 7Native queries are best used:O3
  8. 8A projection is:O4
  9. 9Slice differs from Page by:O4
  10. 10PageRequest.of(0, 20) means:O4

Practical part

Six exercises JNR-029-EX01 to EX06 plus the lab; the assessment is implementing three queries from requirements (EX02, EX04, EX06). Pass: all three assessment queries correct, at least five of six exercises correct, and lab tests green.

Common mistakes

60-character derived method names; native queries by default; returning entities to controllers.

Assessment

Implement three queries from requirements.

Career relevance

Daily work; advanced Hibernate in MID-021.

Type to search levels, phases, modules and lessons.