Skip to content
Ameen Abozaid
MID-021

Advanced Hibernate

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

Overview

Topics

  • Fetch plans
  • batching
  • first/second-level cache
  • entity states

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Design fetch plans per use case with entity graphs and DTO projections.
  2. Configure JDBC batching and explain why identity generation disables insert batching.
  3. Use the second-level cache and query cache appropriately, or decide against them.
  4. Handle detached entities and merging correctly.

Lessons

  1. 01Fetch plans per use case
  2. 02Batching inserts and updates
  3. 03Second-level and query caches
  4. 04Entity states revisited: merge vs persist
  5. 05Hibernate statistics
  6. 06Demo: 10× faster bulk updates with batching

Lab and exercises

Lab

MID-021-LAB1 · Batch job tuning

Outcomes: 1–4 · Time: ~3 h Make Shop's nightly price-update job (100 000 products) 10× faster without leaving JPA: fetch plan, JDBC batching, periodic flush/clear, and (if useful) a cache decision for the category lookup. Acceptance: statement counts and timings before and after, recorded with Hibernate statistics.

Exercises (assessment: tuning exercise)

IDOutcomeTask
MID-021-EX011Fix an N+1 on Order.items with an entity graph and verify the statement count in a test.
MID-021-EX021Write a DTO projection for the order list screen and compare the SQL with loading entities.
MID-021-EX032Configure batching and show the effect on 10 000 updates.
MID-021-EX042Change an IDENTITY id to a sequence and show insert batching starting to work.
MID-021-EX053Decide for five entities whether to use the second-level cache and justify each.
MID-021-EX064Explain what happens with persist, merge, and save on a detached entity, and what each returns.
MID-021-EX074Fix a bug where code modifies the object passed to merge expecting changes to be saved.
Show solutions
  • EX01 @EntityGraph(attributePaths = "items") on the repository method; assert getPrepareStatementCount() equals 1.
  • EX02 select new ...OrderSummary(...): fewer columns, no entity instances, no lazy-loading risk.
  • EX03 batch_size=50, order_updates=true: the statement execution count drops from 10 000 to about 200 batches (measured values vary).
  • EX04 @GeneratedValue(strategy = SEQUENCE) with matching allocationSize and a sequence INCREMENT BY of the same size; Hibernate logs batched inserts.
  • EX05 Cache read-mostly reference data (category, country); avoid stock, orders, and anything written often. Check the reasons given.
  • EX06 persist on a detached entity throws; merge returns a managed copy; save calls merge for entities with an id and returns the saved instance.
  • EX07 Use the returned managed instance: var managed = em.merge(x); managed.setName(...); the original remains detached.
  • The "saveAll without batching" mistake is shown in EX03 and the lab; caching mutable entities in EX05.

Quiz

Outcomes: O1 fetch plans · O2 JDBC batching · O3 caches · O4 entity states and merge.

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. 1The default recommended fetch type for associations:O1
  2. 2An entity graph or JOIN FETCH mainly prevents:O1
  3. 3For a read-only list screen the cheapest option is usually:O1
  4. 4Which id strategy disables JDBC insert batching?O2
  5. 5Why flush and clear in a bulk job?O2
  6. 6hibernate.jdbc.batch_size controls:O2
  7. 7A good second-level cache candidate:O3
  8. 8The query cache is invalidated when:O3
  9. 9merge returns:O4
  10. 10Preferred approach to updating an entity from a DTO:O4

Practical part

Tuning exercise: given a slow job with N+1 loads, IDENTITY ids, and no batching, produce a tuned version with statement counts. Pass: quiz 8/10, three tuning changes made with measured effect, and the lab's before/after figures.

Common mistakes

Caching mutable, frequently written entities; saveAll without batching configured.

Assessment

Tuning exercise.

Career relevance

Choosing persistence tools (MID-022).

Type to search levels, phases, modules and lessons.