Spring Boot services · Chapter 17 of 41

Repositories and query methods

By the end of this chapter you can ask the database exact questions from a repository interface: finds, counts, the top few, your own JPQL, and one update for many rows. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Data
  • 5 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

The station screens are live, and each one asks Tapri Cloud the same questions all day: which orders at SBC are still waiting, is anything ready to hand over, what were the three biggest orders. Ravi also wants a button for the day a counter closes early: cancel everything still waiting there.

The repository from chapter 16 has save, findById and findAll. With only those, every question means loading every order and filtering in Java: fine with seven orders, a disaster with seven lakh. The database is built to filter, sort and count. You need a way to ask it.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    OrderService.java: the waiting orders at a station

    Write OrderService.waiting(station) so it returns the orders at that station whose status is PLACED, in any order: waiting("SBC") gives Masala chai and Bun maska, waiting("NDLS") gives Masala chai, and a station with no orders (CSMT) gives an empty list.

    Let the database filter, with a derived query method you add to OrderRepository. The check counts the orders Hibernate loads: waiting("SBC") may load only the 2 it returns, so findAll and a filter in Java (7 loaded) fail.

  2. Task 2

    OrderService.java: count by status and check for ready orders

    Write countWithStatus(status) so it returns how many orders have that status, at every station together (3 SERVED, and 0 CANCELLED), and anyReady(station) so it returns true when the station has at least one READY order (SBC) and false when it has none (NDLS).

    Neither may load a single Order: the check expects 0 orders loaded across all four calls.

  3. Task 3

    OrderService.java: the three biggest orders at a station

    Write biggestThree(station) so it returns the three orders at that station with the largest amounts, largest first: amounts 120, 90 and 50 at SBC. A station with fewer than three orders gives all of them, largest first (NDLS: 80, then 30).

    Only those three orders may be loaded: the database sorts the rows and stops after three.

  4. Task 4

    OrderService.java: search orders by part of the item name

    Write search(text) so it returns the orders whose item contains the text anywhere, ignoring case, in id order: search("CHAI") gives Masala chai, Cutting chai, Ginger chai and Masala chai, search("maska") gives Bun maska, and search("dosa") gives an empty list.

    Write the query yourself in JPQL, on a repository method with the query annotation.

  5. Task 5 · interview question

    Interview: cancel the waiting orders with one bulk update

    Write cancelWaiting(station) so it sets every PLACED order at that station to CANCELLED with one bulk update query, loading no order, and returns how many orders changed (2 at SBC, then 1 at NDLS). Orders at other stations and the READY order keep their status.

    Make the method run in a transaction, so it works when called with none around it. An order loaded earlier in the same transaction must read back as CANCELLED after the update, not as a stale PLACED. Be ready to explain why a bulk update leaves stale entities behind.

What you will learn

  • Derived queries: Spring Data reads a method name and writes the query.
  • count, exists and Top: answers that bring back a number, a yes or no, or a few rows.
  • @Query: a JPQL query you write yourself.
  • @Modifying: one update for many rows, and the stale entities it leaves (the interview question).

More in Data

Build "Repositories and query methods" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.