Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 119 ยท JPA and Hibernate

Fetching Strategies and the N+1 Problem

Make JPA fast - lazy vs eager loading, the N+1 query problem measured on H2, join fetch, entity graphs, batch fetching, and first- and second-level caching.

An ORM makes loading related data feel free. You write author.getBooks() and the books are just there. But every association you touch may cost a trip to the database, and those trips add up quietly. The classic result is the N+1 problem: one query loads a list of 50 authors, then 50 more queries load their books, one author at a time. On a laptop nobody notices. In production, with network latency and real traffic, the page takes seconds and the database is busy all day.

This lesson shows exactly when Hibernate runs a query, why lazy loading is the right default, and how to load what you need in one or a few queries. Every example counts the SQL statements it sends to a real H2 database, so you can see the difference instead of taking it on faith. You should know the mappings from Entity Relationships and basic JPQL from Querying with JPQL and the Criteria API.

Lazy and eager loading

Every association has a fetch type that says when its data is loaded:

  • FetchType.EAGER: load the association together with the owning entity, always.
  • FetchType.LAZY: load it the first time your code actually uses it.

The JPA defaults are not consistent, and you should know them by heart:

Mapping Default fetch type
@ManyToOne, @OneToOne EAGER
@OneToMany, @ManyToMany LAZY
@ElementCollection LAZY
basic fields (String, int, ...) EAGER

The rule professionals follow is simple: make every association lazy, then fetch eagerly per query where a use case needs the data.

Java
@ManyToOne(fetch = FetchType.LAZY)
private Customer customer;

Why not just make everything eager? Because eager is a decision made once, for every use case. A Book that eagerly loads its Author, which eagerly loads its Publisher, turns "show the book title" into three tables' worth of work. Worse, EAGER does not mean "use a join". When you load books with a JPQL query, Hibernate runs your query first and then fetches each eager association it finds, often one extra query per distinct author. Eager mappings are a common hidden source of N+1 queries.

How lazy loading works

For a lazy @ManyToOne, Hibernate puts a proxy in the field: an object of a generated subclass of Customer that knows only the id. Calling getId() is free; calling any other getter makes the proxy run a query and fill itself in. Lazy collections are Hibernate's own List and Set implementations (PersistentBag, PersistentSet) that load their elements on the first call to size(), iterator() and so on.

That loading needs an open persistence context (Hibernate's Session). Touch a lazy association after the session is closed, typically in a view or a JSON serializer after the transaction ended, and you get the famous LazyInitializationException. Here is a small lazy collection that behaves the same way:

Java A lazy collection and LazyInitializationException

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Ted's books were loaded once, on first use, and reused after that. Kazuo's books were never touched while the session was open, so they could not be loaded afterwards. The fix is never "make it eager" or "keep the session open longer". It is to load what the use case needs before the transaction ends, with the techniques below.

Spring Boot enables Open Session in View by default (spring.jpa.open-in-view=true, and it logs a warning at startup about it). It keeps the persistence context open until the HTTP response is written, so lazy loading "just works" in views and JSON. It also hides N+1 queries and holds a database connection for the whole request. Most teams set spring.jpa.open-in-view=false and fetch explicitly.

The N+1 problem, measured

Here is the pattern in JPA code. It looks innocent:

Java
List<Author> authors = entityManager
    .createQuery("select a from Author a order by a.id", Author.class)
    .getResultList();                       // 1 query

for (Author author : authors) {
    System.out.println(author.getName() + ": " + author.getBooks().size());   // 1 query EACH
}

The next example runs exactly the SQL Hibernate would send for 20 authors with 3 books each, and counts it.

Java The N+1 problem with a query counter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Twenty-one queries for one screen: one for the list, plus one per author. Each statement is fast, but each is a full network round trip, a parse and a plan on the server. With 1 ms of latency and 500 authors, that is half a second of pure waiting, and it grows linearly with your data. The rest of this lesson shows three ways to bring the number down.

Fix 1: join fetch

A fetch join tells Hibernate to load the association in the same SQL statement:

Java
List<Author> authors = entityManager.createQuery("""
        select a from Author a
        left join fetch a.books
        order by a.id
        """, Author.class)
    .getResultList();

The SQL is a single LEFT JOIN, and Hibernate assembles each author's collection from the rows. Use left join fetch when the association may be empty (otherwise authors without books disappear) and join fetch when it never is. Since Hibernate 6, duplicate root entities caused by the join are removed automatically, so you no longer need select distinct.

Java One query with a fetch join

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The first query did all the work of the previous example in one statement. Look at the second number, though: joining two collections in one query produces a cartesian product. Each author's 3 books are repeated for each of their 2 awards, so the database returns 120 rows for 60 books and 40 awards. With realistic sizes that explodes quickly. Hibernate even refuses to fetch two List collections ("bags") at once and throws MultipleBagFetchException. Fetch one collection per query, or use batch fetching for the others.

Fetch joins have a second limit, mentioned in the previous lesson: combined with setMaxResults, a collection fetch join forces Hibernate to load all rows and page in memory. Page the parent ids first, then fetch their collections.

Fix 2: entity graphs

A fetch join hard-codes the fetching into the query text. An entity graph describes what to load separately, so the same query can be reused with different fetch plans:

Java
@Entity
@NamedEntityGraph(name = "Author.withBooks", attributeNodes = @NamedAttributeNode("books"))
public class Author { ... }

List<Author> authors = entityManager.createQuery("select a from Author a order by a.id", Author.class)
    .setHint("jakarta.persistence.fetchgraph", entityManager.getEntityGraph("Author.withBooks"))
    .getResultList();

Author one = entityManager.find(Author.class, 7L,
    Map.of("jakarta.persistence.fetchgraph", entityManager.getEntityGraph("Author.withBooks")));

Hibernate turns the graph into the same left join. With the fetchgraph hint, attributes not in the graph are treated as lazy; the loadgraph hint keeps their mapped fetch types instead. In Spring Data JPA you usually write the shortcut @EntityGraph(attributePaths = "books") on a repository method.

Fix 3: batch fetching

Batch fetching keeps the code from the N+1 example exactly as it was. When Hibernate needs to initialize one lazy collection, it initializes up to n of them at once, for other authors already in the persistence context, with an IN list:

Java
@OneToMany(mappedBy = "author")
@BatchSize(size = 8)              // org.hibernate.annotations.BatchSize
private List<Book> books;

Or globally, which many teams enable on every project:

PROPERTIES
hibernate.default_batch_fetch_size=16

With 20 authors and a batch size of 8, that is 1 + 3 queries instead of 1 + 20.

Java Batch fetching with IN lists

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Batch fetching is a great safety net because it needs no changes to queries, and it also helps with lazy @ManyToOne proxies. Depending on the database, Hibernate 6 may send one array parameter (where author_id = any (?)) instead of a list of ?s, but the effect is the same. A related option, @Fetch(FetchMode.SUBSELECT), loads the collections of all authors from the original query with one subquery: always exactly two queries.

Fix 4: don't load entities at all

For read-only screens and API responses, the fastest fix is often a DTO projection: select exactly the columns the page shows.

Java
record AuthorSummary(String name, Long bookCount) {}

List<AuthorSummary> rows = entityManager.createQuery("""
        select new com.example.catalog.AuthorSummary(a.name, count(b))
        from Author a left join a.books b
        group by a.id, a.name
        order by a.name
        """, AuthorSummary.class)
    .getResultList();

One query, no entities in the persistence context, no lazy loading to worry about, and less data on the wire. Load entities when you are going to change them; use projections when you only show them.

Choosing a strategy

Situation Good choice
One screen needs an entity plus one association join fetch or an entity graph
Many lazy associations touched in a loop @BatchSize / hibernate.default_batch_fetch_size
Several collections of the same parent one fetch join + batch fetching for the rest
Read-only list, report or JSON API DTO projection
A page of parents with their children page the ids, then fetch by ids

Caching: first and second level

The first-level cache

Every persistence context is also an identity map: within one transaction, each database row is represented by exactly one Java object. Call find(Author.class, 1L) twice and the second call returns the same object without SQL. This "first-level cache" is always on and lives only as long as the persistence context.

It has a subtle consequence: a JPQL query always runs SQL, but if a returned row belongs to an entity that is already managed, Hibernate gives you the existing object and ignores the new column values. Changes made by someone else during your transaction stay invisible until you call refresh() or start a new persistence context.

Java The persistence context as an identity map

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The identity map is why == works between entities of the same transaction and why loading the same author 50 times in a loop costs one query. It is also why a long-lived persistence context slowly fills with stale objects and memory. Keep units of work short, and call entityManager.clear() in long batch jobs.

The second-level cache

The second-level cache is shared by all persistence contexts in the application (and, with a distributed provider, by several servers). It stores entity data by id, so find() in a new transaction can skip the database. It is off by default and is enabled per entity:

PROPERTIES
hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=jcache
jakarta.persistence.sharedCache.mode=ENABLE_SELECTIVE
Java
@Entity
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Country { ... }

You also need a provider on the classpath, such as Ehcache 3, Caffeine or Infinispan through hibernate-jcache. The second-level cache works best for read-mostly reference data: countries, currencies, categories, configuration. For data that changes often, cache invalidation costs more than it saves, and a cache shared across servers must be kept consistent. Hibernate also has a query cache that stores the ids returned by a query, but it is invalidated whenever any table in the query changes, so it rarely pays off. Measure first; many applications get further with good queries and an application-level cache (see Scheduling, Caching and Async Tasks).

Seeing the SQL

You can't fix queries you can't see. During development, turn on SQL logging and statistics:

PROPERTIES
# the SQL statements (Hibernate 6 logger names)
logging.level.org.hibernate.SQL=debug
# the bound parameter values
logging.level.org.hibernate.orm.jdbc.bind=trace
# query counts and timings per session
spring.jpa.properties.hibernate.generate_statistics=true

Better still, write a test that asserts the number of queries for important screens, using Hibernate's Statistics API or a JDBC proxy library such as datasource-proxy. An N+1 regression then fails the build instead of slowing down production.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the default fetch type of a @ManyToOne association?

    ToOne associations (@ManyToOne, @OneToOne) are eager by default; collections are lazy. Most teams set every association to LAZY explicitly.
  2. 2. A query loads 100 orders, and the code then calls order.getLines().size() on each one with no batch fetching. How many queries run?

    One query for the orders plus one per order to initialize each lazy collection. That is the N+1 problem.
  3. 3. You read author.getBooks() in a JSON serializer after the transaction has committed and the session is closed. The collection was never touched before. What happens?

    A lazy collection needs an open session to load. Fetch what the response needs inside the transaction, for example with a fetch join or a DTO.
  4. 4. Why can fetching two collections of the same entity in one query be a bad idea?

    Every row of one collection is combined with every row of the other. Hibernate even rejects fetching two bags with MultipleBagFetchException.
  5. 5. Inside one transaction you load author 1, another application renames it, and you run a JPQL query that returns author 1 again. Which name do you see?

    The query does run, but the identity map returns the existing managed instance and ignores the new column values. Use refresh() or a new persistence context to see the change.

Summary

  • Associations are eager (@ManyToOne, @OneToOne) or lazy (collections) by default. Make everything lazy and fetch per use case.
  • Lazy associations are proxies and smart collections; touching them after the session closed throws LazyInitializationException.
  • The N+1 problem is one query for a list plus one per element. Count your queries to find it.
  • Fix it with join fetch or entity graphs (one query), batch fetching (a few IN queries), or a DTO projection (one query, no entities).
  • Fetch at most one collection per query to avoid cartesian products, and don't combine collection fetch joins with paging.
  • The first-level cache is the persistence context's identity map; the second-level cache is optional, shared and best for read-mostly data.
  • Turn on SQL logging in development and test query counts for important screens.

Next: Entity Lifecycle and Transactions explains when Hibernate writes your changes and how transactions and optimistic locking protect them.