Find N+1 Queries in JPA Code
ExoBench runs your Hibernate or EclipseLink mappings on the real ORM and returns every SQL statement they prepared, with N+1 findings.
ExoBench can run your JPA code and show you the SQL it really sends. You give your AI assistant an entity mapping and the code that loads it. ExoBench compiles them, runs them on the real ORM against a throwaway database, and returns every statement the ORM prepared, in order, with any N+1 it found.
It answers the question a code review cannot: not "does this mapping look right" but "how many statements did it send".
What You Type
Here are my JPA entities and the method that loads them. Run them on Hibernate 6.6 against a few rows of seed data and show me every SQL statement that was prepared. Flag any N+1.
Paste the entity classes and the method after it. Your assistant writes the seed rows and calls ExoBench.
What Comes Back
Two entities, Parent with a lazy @OneToMany to Child, three parents with one child each. The code lists the parents and reads each one's children. This is the transcript ExoBench returned for it on Hibernate 6.6.55:
select p1_0.id,p1_0.name from parent p1_0
select c1_0.parent_id,c1_0.id,c1_0.label from child c1_0 where c1_0.parent_id=?
select c1_0.parent_id,c1_0.id,c1_0.label from child c1_0 where c1_0.parent_id=?
select c1_0.parent_id,c1_0.id,c1_0.label from child c1_0 where c1_0.parent_id=?
One statement for the parents, then the same statement three times, once per parent. ExoBench flags it:
{
"kind": "nPlusOneSuspect",
"shape": "select c1_0.parent_id,c1_0.id,c1_0.label from child c1_0 where c1_0.parent_id=?",
"repetitions": 3,
"firstIndex": 1,
"hint": "Repeated select after a different driving statement suggests N+1. Consider JOIN FETCH, @BatchSize, @EntityGraph, or Hibernate.initialize on a batch."
}
Four prepared statements for three parents: one more for every parent the query returns. Your assistant then applies a fix and runs it again, and the transcript shows whether the count went down.
Which ORMs
| ORM | Version | JPA | Namespace |
|---|---|---|---|
| Hibernate | 5.6.15 | 2.2 | javax.persistence |
| Hibernate | 6.2.52 | 3.1 | jakarta.persistence |
| Hibernate | 6.6.55 (default) | 3.1 | jakarta.persistence |
| Hibernate | 7.0.10 | 3.2 | jakarta.persistence |
| EclipseLink | 2.7.16 | 2.2 | javax.persistence |
| EclipseLink | 3.0.4 | 3.0 | jakarta.persistence |
| EclipseLink | 4.0.9 | 3.1 | jakarta.persistence |
| EclipseLink | 5.0.1 | 3.2 | jakarta.persistence |
Kotlin or Java. The same entities run unchanged on Hibernate 6.2, 6.6 and 7.0, so you can see whether an upgrade changes the SQL.
What the Sandbox Is
- The database is an in-memory H2 that exists for one run. Nothing persists, and nothing connects to your database.
- The second-level cache is off, so every load is the uncached truth.
- JDBC batching is not configured, so every write shows up as its own statement.
- Hibernate bytecode enhancement is off unless you ask for it, which matches most builds. EclipseLink classes are woven, which matches a woven deployment.
The transcript tells you what the ORM sends. It does not tell you how long those statements take on your data. For that, hand one of the statements to ExoBench's SQL benchmark and run it on Postgres at your real row counts.
What a Transcript Proves, and What It Does Not
A transcript is a measurement of one code path on one set of rows. JOIN FETCH May Not Save You covers the limits, including a case where the tool was wrong.
Set It Up
The analyzer is part of ExoBench, and it runs from your own AI tool: Claude, Cursor, or any client that speaks MCP. The in-browser chat on the home page benchmarks SQL only. Connect ExoBench to your AI tool, sign in with GitHub, and ask in your own words. Now in preview, no card needed.
To see it at work on real code, read JOIN FETCH May Not Save You: seven beliefs about N+1, checked against Shopizer, Apache Fineract and Spring PetClinic.