Spring Boot services · Chapter 22 of 41
The persistence context
By the end of this chapter you can say which objects Hibernate is watching, write changes without save, and choose when changes reach the database and when they never do. 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
Meera tried the new "what if" screen: what would Masala chai sell like at 60 rupees? Ten minutes later the counters were charging 60 for real, and nobody had called save. In the same week Ravi's price change through the admin form did nothing at all, and the "under 30 rupees" banner kept counting an item that no longer cost under 30.
All three come from one place. Hibernate does not write objects when you ask: it watches the objects it loaded inside a transaction and writes whatever changed. Until you know which objects it watches, and when it writes, it looks like magic that sometimes misfires. In this chapter MenuItemRepository has only findById, on purpose: no save at all.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1 · interview question
Interview: change a price without calling save
Write MenuService.changePrice(id, price) so the item's new price reaches the database: after changePrice(Cutting chai's id, 30) the menu_items table says 30 for Cutting chai, and Masala chai still says 40.
The repository has only findById, and no save is needed: find the item and change its price. Be ready to explain why the change is written without a save call.
- Task 2
MenuService.java: two lookups of one item give the same object
Make lookUpTwice(id) return a list of two entries where the second is the very same instance as the first (Bun maska both times), because both lookups ran in one persistence context.
Keep its two findById calls as they are.
- Task 3
MenuService.java: write a detached item's changes with merge
Make update(edited) write the state of a detached item (read with find, then changed, such as Masala chai at 45) to the database, and return the managed copy: the object the persistence context holds, not the object passed in.
Afterwards the database says 45, the returned item's price is 45, and the returned item is a different instance from edited.
- Task 4
MenuService.java: flush before the plain SQL count
Make changePriceAndCountBelow(id, price, limit) send the price change to the database before the JdbcClient count runs, in the same transaction, so the count sees the new price.
With Cutting chai 20, Masala chai 40 and Bun maska 50: changing Cutting chai to 30 counts 0 items under 30 (and the database keeps 30); changing Masala chai to 25 counts 1.
- Task 5
MenuService.java: preview a price without writing it
Make previewPrice(id, price) return the item with the new price and its name (Masala chai at 60) while the database keeps the old price (40).
Take the item out of the persistence context before you change its price, so nothing watches the change.
What you will learn
- The persistence context and dirty checking: the objects a transaction has loaded, and how changes to them are written.
- The first-level cache: one object per row inside a transaction.
- Detached entities and merge: changes made outside, written back.
- Flush and detach: when SQL is sent, and how to keep a change in memory only.
More in Data
Build The persistence context 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.