Skip to content

codingkiddo/hibernate-n-plus-one-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hibernate N+1 Demo (Spring Boot 3 / Hibernate 6)

A tiny, copy‑pastable project that reproduces the N+1 problem and shows 3 safe fixes:

  • Baseline (trigger N+1): findAll() then access author.getBooks() in a loop.
  • Fix #1: JOIN FETCH query
  • Fix #2: @EntityGraph(attributePaths = "books")
  • (Bonus) hibernate.default_batch_fetch_size is configured to help with to‑one proxies.

How to run

mvn -v          # Java 21 + Maven 3.9+ recommended
mvn test -q

The tests assert query counts using Hibernate Statistics:

  • baselineShowsNPlusOne() expects >= 6 statements with our seed data (5 authors).
  • joinFetchFixUsesSingleQuery() and entityGraphFixUsesSingleQuery() expect <= 2 statements.

Key files

  • AuthorBook with @OneToMany / @ManyToOne (LAZY)
  • AuthorRepository with:
    • findAllWithBooksJoinFetch()select distinct a from Author a left join fetch a.books
    • findAllWithBooksGraph()@EntityGraph(attributePaths="books")
    • IDs-then-fetch helpers to implement paging without N+1
  • DemoService — methods that reproduce/fix N+1 and expose a helper to read the current prepareStatement count
  • application.ymlopen-in-view: false, SQL logging, and hibernate.default_batch_fetch_size: 50
  • application-test.ymlhibernate.generate_statistics: true
  • data.sql — seeds 5 authors × 3 books

Extending (pagination-safe two-step)

When you need pagination with a to-many association, use the two-step pattern:

  1. Page parent IDs (Page<Long>)
  2. Fetch the graph with join fetch for where id in :ids

See AuthorRepository.pageIdsByNamePrefix(...) and fetchGraphByIds(ids).

Why distinct?

Join fetching a collection duplicates parent rows in SQL. select distinct avoids exploding row counts and lets Hibernate deduplicate entities safely.


Happy debugging! If you want me to wire this into your existing service with concrete entity names, say the word.

About

Hibernate N+1 Demo (Spring Boot 3 / Hibernate 6)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages