Spring Data JPA
On this page 8 topics
Overview
Topics
- Repositories
- derived queries
- JPQL
- native queries
Learn first
3 h of practice (estimate)
Learning outcomes
- Define repositories extending
JpaRepositoryand explain the generated implementation. - Write derived queries and recognize when a name becomes unreadable.
- Write JPQL and native queries with
@Queryand parameters. - Return projections (interface or record) for read-only views and use
Pageable.
Lessons
Lab and exercises
Lab
JNR-029-LAB1 · Catalog on JPA
Outcomes: 1–4 · Time: ~3 h
- Move products and categories to Spring Data JPA repositories.
- Add
findBySku(derived) and a paged search by name, category, and price range (@Query). - Return a
ProductListItemprojection for the listing; map to the API response in the service. - Keep all existing endpoints and JSON unchanged.
- 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)
| ID | Outcome | Task |
|---|---|---|
| JNR-029-EX01 | 1 | Declare CustomerRepository and use findById, save, count; explain who implements it. |
| JNR-029-EX02 | 2 | Write derived queries: products by status, products with name containing text (ignore case). |
| JNR-029-EX03 | 2 | Rewrite a 60-character derived method as a @Query. |
| JNR-029-EX04 | 3 | Write a JPQL query with named parameters: orders of a customer between two dates. |
| JNR-029-EX05 | 3 | Write a native query counting products per category and explain why native was chosen. |
| JNR-029-EX06 | 4 | Return 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);andList<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_idwithnativeQuery = true; chosen for a reporting aggregate returned as a projection, not because it is the default. - EX06
Page<ProductListItem> list(Pageable p);called withPageRequest.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.
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.