Add n-plus-one-guard module: detecting N+1 with Hibernate Statistics vs datasource-proxy, five fixes with statement counts, and a JUnit 6 @MaxSelects guard (Hibernate 7.4.16.Final, datasource-proxy 1.11.1)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6R1spapM7nBteaqeo
This commit is contained in:
asmhatre
2026-11-17 00:48:25 +05:30
co-authored by Claude Sonnet 5.5
parent eab5461d86
commit 0db721805b
20 changed files with 851 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
# N+1 on a lazy collection: 10 authors, 3 books each
# Hibernate ORM 7.4.16.Final, datasource-proxy 1.11.1, H2, JDK 25
== Code: one HQL query, then a.getBooks().size() for every author
books counted by the loop : 30
datasource-proxy SELECT count : 11
Hibernate prepareStatementCount : 11
Hibernate queryExecutionCount : 1
Hibernate collectionFetchCount : 10
Hibernate collectionLoadCount : 10
== The statements, in order
sql> select a1_0.id,a1_0.name from Author a1_0
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
@@ -0,0 +1,19 @@
# N+1 on a lazy @ManyToOne: 30 books, 10 distinct authors
# Hibernate ORM 7.4.16.Final, datasource-proxy 1.11.1, H2, JDK 25
== Code: one HQL query for 30 books, then book.getAuthor().getName() for every book
datasource-proxy SELECT count : 11
Hibernate entityFetchCount : 10
== The statements, in order
sql> select b1_0.id,b1_0.author_id,b1_0.title from Book b1_0
sql> select a1_0.id,a1_0.name from Author a1_0 where a1_0.id=?
sql> select a1_0.id,a1_0.name from Author a1_0 where a1_0.id=?
sql> select a1_0.id,a1_0.name from Author a1_0 where a1_0.id=?
sql> select a1_0.id,a1_0.name from Author a1_0 where a1_0.id=?
sql> select a1_0.id,a1_0.name from Author a1_0 where a1_0.id=?
sql> select a1_0.id,a1_0.name from Author a1_0 where a1_0.id=?
sql> select a1_0.id,a1_0.name from Author a1_0 where a1_0.id=?
sql> select a1_0.id,a1_0.name from Author a1_0 where a1_0.id=?
sql> select a1_0.id,a1_0.name from Author a1_0 where a1_0.id=?
sql> select a1_0.id,a1_0.name from Author a1_0 where a1_0.id=?
+36
View File
@@ -0,0 +1,36 @@
# Five ways to load 10 authors with their books
# Hibernate ORM 7.4.16.Final, datasource-proxy 1.11.1, H2, JDK 25
== naive loop (lazy collection per author)
sql> select a1_0.id,a1_0.name from Author a1_0
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
== join fetch
sql> select a1_0.id,b1_0.author_id,b1_0.id,b1_0.title,a1_0.name from Author a1_0 join Book b1_0 on a1_0.id=b1_0.author_id
== entity graph (Author.withBooks)
sql> select a1_0.id,b1_0.author_id,b1_0.id,b1_0.title,a1_0.name from Author a1_0 left join Book b1_0 on a1_0.id=b1_0.author_id
== batch fetching, size 5 (session.setFetchBatchSize)
sql> select a1_0.id,a1_0.name from Author a1_0
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id in (?,?,?,?,?)
sql> select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id in (?,?,?,?,?)
== DTO projection (no entities)
sql> select count(b1_0.id) from Book b1_0 group by b1_0.author_id
== SELECT statements for 10 authors x 3 books, same result every time (30 books)
naive loop (lazy collection per author) 11
join fetch 1
entity graph (Author.withBooks) 1
batch fetching, size 5 (session.setFetchBatchSize) 3
DTO projection (no entities) 1
@@ -0,0 +1,5 @@
# join fetch on a collection + setMaxResults(3), 10 authors x 3 books
# Hibernate ORM 7.4.16.Final, datasource-proxy 1.11.1, H2, JDK 25
authors returned: 3, books loaded: 9
SELECT statements: 1
sql> select a1_0.id,b1_0.author_id,b1_0.id,b1_0.title,a1_0.name from (select a1_0.id,a1_0.name from Author a1_0 where exists(select 1 from Book b1_0 where a1_0.id=b1_0.author_id) order by a1_0.id offset ? rows fetch first ? rows only) a1_0(id,name) join Book b1_0 on a1_0.id=b1_0.author_id order by a1_0.id
@@ -0,0 +1,16 @@
# What the guard prints when a test regresses (BrokenFixture, @MaxSelects(2))
# Hibernate ORM 7.4.16.Final, datasource-proxy 1.11.1, H2, JDK 25
== Failure message
listAuthorsWithBookCount(): expected at most 2 SELECT statement(s) but 11 were sent.
1) select a1_0.id,a1_0.name from Author a1_0
2) select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
3) select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
4) select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
5) select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
6) select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
7) select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
8) select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
9) select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
10) select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?
11) select b1_0.author_id,b1_0.id,b1_0.title from Book b1_0 where b1_0.author_id=?