JAVA-250 · Lesson 10 of 12 · 25 min
Lazy loading and performance
▶ Video coming soon A video version of this lesson is on the way.
ShopBook's first JPA version went to the Columbus desk for a trial in the week before Friday, November 26, and Odile reported that the bookings page, which had opened instantly with the old DAO, now took four seconds on a busy Saturday. Hamza turned on Hibernate's SQL log and counted. One page view sent 61 queries: one for the bookings and then one for each booking's bike, customer record and invoice. It is the most famous performance problem in ORM programming, the N+1 select problem, and this lesson explains where it comes from, how lazy and eager loading work, how to fetch exactly what a screen needs, and how indexes make the remaining queries fast.
Lazy and eager loading
When JPA loads an entity, it has to decide what to do about the entities it refers to. Eager loading fetches them at the same time, usually with a join. Lazy loading leaves them out and fetches each one the first time the program touches it. Each relationship has a fetch type, and the defaults in the specification are a historical accident worth memorizing: @ManyToOne and @OneToOne are EAGER by default, while @OneToMany and @ManyToMany are LAZY. The common advice, including from the Hibernate team, is to make every relationship lazy, as Booking.bike was in lesson 8, and to ask for related data explicitly in each query that needs it.
| Relationship | Default fetch type | Usual recommendation |
|---|---|---|
| @ManyToOne | EAGER | Set fetch = FetchType.LAZY |
| @OneToOne | EAGER | Set LAZY; on the inverse side Hibernate may still need a query |
| @OneToMany | LAZY | Keep LAZY |
| @ManyToMany | LAZY | Keep LAZY |
| Basic fields | EAGER | Keep; lazy basic fields need bytecode enhancement |
How can a field be loaded later if the program holds an ordinary reference to it? The provider puts a proxy there: an object of a generated subclass of the entity that knows only the id. Calling getId on it answers from that id; calling any other method makes the proxy load the real row, once, and then delegate to it. Collections are replaced by the provider's own collection classes that load their contents on first use in the same way. Hibernate generates proxy classes with ByteBuddy. The standard library's java.lang.reflect.Proxy can do the same trick for interfaces, which the example below uses to count the queries a lazy reference causes.
import java.lang.reflect.Proxy;
import java.util.*;
public class Main {
interface Bike { String tag(); String kind(); }
record RealBike(String tag, String kind) implements Bike {}
static final Map<String, String> BIKE_TABLE = Map.of("SG-0007-AR", "CARGO", "SG-0012-TA", "TANDEM", "SG-0003-CI", "CITY");
static int queries = 0;
static Bike lazyBike(String tag) {
Bike[] target = new Bike[1];
return (Bike) Proxy.newProxyInstance(Bike.class.getClassLoader(), new Class<?>[]{Bike.class}, (proxy, method, args) -> {
if (method.getName().equals("tag")) return tag; // the id is already known
if (target[0] == null) {
queries++;
System.out.println(" SELECT * FROM bike WHERE tag = '" + tag + "'");
target[0] = new RealBike(tag, BIKE_TABLE.get(tag));
}
return method.invoke(target[0], args);
});
}
record Booking(int id, String customer, Bike bike) {}
public static void main(String[] args) {
List<Booking> bookings = List.of(new Booking(3, "Mei", lazyBike("SG-0003-CI")),
new Booking(4, "Ana", lazyBike("SG-0007-AR")), new Booking(5, "Theo", lazyBike("SG-0003-CI")));
queries = 1; // the query that loaded the bookings
System.out.println("Loaded " + bookings.size() + " bookings");
for (Booking b : bookings) System.out.println(b.customer() + " -> " + b.bike().tag());
System.out.println("Queries after reading tags: " + queries);
for (Booking b : bookings) System.out.println(b.customer() + " -> " + b.bike().kind());
System.out.println("Queries after reading kinds: " + queries);
}
}Loaded 3 bookings Mei -> SG-0003-CI Ana -> SG-0007-AR Theo -> SG-0003-CI Queries after reading tags: 1 SELECT * FROM bike WHERE tag = 'SG-0003-CI' Mei -> CITY SELECT * FROM bike WHERE tag = 'SG-0007-AR' Ana -> CARGO SELECT * FROM bike WHERE tag = 'SG-0003-CI' Theo -> CITY Queries after reading kinds: 4
What this code does
Reading each bike's tag costs nothing, because the proxy already holds the id. Reading the kind makes each proxy run its own SELECT, so three bookings cost three more queries, one per proxy, even though Mei and Theo rent the same bike: these toy proxies do not share a persistence context. In Hibernate the context would load SG-0003-CI once, but every distinct bike would still cost one query.
The N+1 select problem
That pattern, one query for a list and then one more for each element, is the N+1 select problem. For the bookings page, N was 20 bookings and each touched three lazy references, giving 61 queries. Each query is fast on its own; the cost is the round trips, and on a network with a millisecond of latency each, plus pool waits on a busy day, sixty of them add up to seconds. Eager loading is not the cure: an EAGER relationship is loaded even by screens that never use it, and when entities are loaded by a JPQL query, Hibernate often fetches eager relationships with extra selects afterwards, which is the same N+1 problem in another place.
The cure is to say, in the query for each use case, what that use case needs. JOIN FETCH in JPQL loads the related entities in the same SQL statement with a join, and fills the references with real objects instead of proxies. The bookings page now asks for its bookings and their bikes in one query.
List<Booking> page = em.createQuery("""
SELECT b FROM Booking b
JOIN FETCH b.bike
WHERE b.startDate >= :from
ORDER BY b.startDate, b.id
""", Booking.class)
.setParameter("from", LocalDate.of(2027, 10, 1))
.getResultList();What this code does
JOIN FETCH b.bike tells the provider to select the bike columns in the same statement and build real Bike objects, so the loop that prints each bike's kind sends no more queries. The relationship stays LAZY in the mapping; only this query asks for it. Other screens that do not need bikes keep loading bookings alone.
Hibernate: select b1_0.id,b1_0.bike_id,b2_0.id,b2_0.kind,b2_0.tag,b1_0.customer,b1_0.end_date,b1_0.start_date,b1_0.version from booking b1_0 join bike b2_0 on b2_0.id=b1_0.bike_id where b1_0.start_date>=? order by b1_0.start_date,b1_0.idWhat this code does
One statement with a join replaces the twenty bike lookups of the original page. This is representative output with show_sql enabled; the column order and aliases are chosen by Hibernate.
The difference is easy to measure without a database. The example below counts the queries of each strategy for twenty bookings spread over the shop's six bikes.
import java.util.*;
public class Main {
record Booking(int id, String bikeTag) {}
static int queries;
static Map<String, String> loadBikesOneByOne(List<Booking> bookings, boolean contextCache) {
Map<String, String> loaded = new HashMap<>();
for (Booking b : bookings) {
if (contextCache && loaded.containsKey(b.bikeTag())) continue; // already in the persistence context
queries++; // SELECT ... WHERE id = ?
loaded.put(b.bikeTag(), "bike");
}
return loaded;
}
static void loadBikesInBatches(List<Booking> bookings, int batchSize) {
List<String> distinct = bookings.stream().map(Booking::bikeTag).distinct().toList();
queries += (distinct.size() + batchSize - 1) / batchSize; // SELECT ... WHERE id IN (?, ?, ...)
}
public static void main(String[] args) {
String[] tags = {"SG-0001-CI", "SG-0002-CI", "SG-0003-CI", "SG-0004-CI", "SG-0007-AR", "SG-0012-TA"};
List<Booking> bookings = new ArrayList<>();
for (int i = 0; i < 20; i++) bookings.add(new Booking(100 + i, tags[i % tags.length]));
queries = 1; loadBikesOneByOne(bookings, false);
System.out.println("N+1 without a shared context: " + queries);
queries = 1; loadBikesOneByOne(bookings, true);
System.out.println("N+1 with a persistence context: " + queries);
queries = 1; loadBikesInBatches(bookings, 4);
System.out.println("Batch fetching, size 4: " + queries);
queries = 1;
System.out.println("JOIN FETCH: " + queries);
}
}N+1 without a shared context: 21 N+1 with a persistence context: 7 Batch fetching, size 4: 3 JOIN FETCH: 1
What this code does
Without any sharing, every booking costs a query, 21 in all. A persistence context loads each of the six distinct bikes once, giving 7, and batch fetching with a batch size of 4 needs two IN queries for six bikes, giving 3. JOIN FETCH does all of it in the first query.
Other ways to fetch
JOIN FETCH is the simplest fix, but there are others. An entity graph names the relationships to load and is passed to find or to a query as a hint, so the same JPQL can be used with different fetch plans. Batch fetching, Hibernate's @BatchSize or the hibernate.default_batch_fetch_size setting, keeps lazy loading but loads proxies of the same type in groups with an IN list, which turns N+1 into N divided by the batch size plus one with no change to the queries. And for read-only screens a projection into a record (lesson 9) avoids entities, proxies and the persistence context altogether.
// an entity graph: the same query, a different fetch plan
EntityGraph<Booking> withBike = em.createEntityGraph(Booking.class);
withBike.addAttributeNodes("bike");
List<Booking> bookings = em.createQuery("SELECT b FROM Booking b ORDER BY b.id", Booking.class)
.setHint("jakarta.persistence.fetchgraph", withBike)
.getResultList();
// batch fetching for every lazy association, in persistence.xml or application properties
// hibernate.default_batch_fetch_size = 16What this code does
The fetch graph says that bike must be loaded with each booking, and the provider adds the join itself. The hint name jakarta.persistence.fetchgraph treats attributes not listed as lazy, while jakarta.persistence.loadgraph keeps the mapping's own defaults for them. A default batch size is a cheap safety net for the lazy loads that remain.
Fetching a collection with JOIN FETCH needs care. Joining a bike to its bookings returns one SQL row per booking, so the bike appears once per booking in the raw result; Hibernate 6 and later remove those duplicates from entity results automatically, which older versions needed DISTINCT for. More seriously, combining JOIN FETCH of a collection with setMaxResults cannot be paged in SQL, because a page of joined rows is not a page of bikes, so Hibernate fetches everything and pages in memory, logging a warning that says so. Fetch collections for a page with batch fetching, or page the parents first and fetch their children in a second query.
LazyInitializationException
A lazy proxy can load only while its persistence context is open. If the EntityManager has been closed, for example because the request's transaction ended before the page template ran, touching an unloaded proxy throws Hibernate's LazyInitializationException: could not initialize proxy, no Session. The tempting fixes are both wrong. Making the relationship EAGER loads it everywhere. Keeping the EntityManager open until the page is rendered, a pattern called open session in view, hides N+1 queries in the template and holds connections longer; Spring Boot enables it by default and logs a warning about it at startup. The right fix is to fetch what the view needs, with JOIN FETCH, an entity graph or a projection, while the transaction is open.
import java.util.*;
import java.util.function.Supplier;
public class Main {
static class LazyInitializationException extends RuntimeException {
LazyInitializationException(String msg) { super(msg); }
}
static class Session { boolean open = true; int queries = 0; }
static class LazyRef<T> {
private final Session session; private final Supplier<T> loader; private final String what;
private T value; private boolean loaded;
LazyRef(Session s, String what, Supplier<T> loader) { session = s; this.what = what; this.loader = loader; }
static <T> LazyRef<T> fetched(Session s, String what, T value) {
LazyRef<T> r = new LazyRef<>(s, what, () -> value); r.value = value; r.loaded = true; return r;
}
T get() {
if (!loaded) {
if (!session.open) throw new LazyInitializationException("could not initialize proxy [" + what + "] - no Session");
session.queries++; value = loader.get(); loaded = true;
}
return value;
}
}
record Booking(String customer, LazyRef<String> bikeKind) {}
public static void main(String[] args) {
Session s1 = new Session();
Booking lazy = new Booking("Ana", new LazyRef<>(s1, "Bike#SG-0007-AR", () -> "CARGO"));
s1.open = false; // the transaction ended
try {
System.out.println(lazy.customer() + ": " + lazy.bikeKind().get());
} catch (LazyInitializationException e) {
System.out.println("Rendering failed: " + e.getMessage());
}
Session s2 = new Session();
Booking fetched = new Booking("Ana", LazyRef.fetched(s2, "Bike#SG-0007-AR", "CARGO")); // JOIN FETCH
s2.open = false;
System.out.println(fetched.customer() + ": " + fetched.bikeKind().get() + " (extra queries: " + s2.queries + ")");
}
}Rendering failed: could not initialize proxy [Bike#SG-0007-AR] - no Session Ana: CARGO (extra queries: 0)
What this code does
The first booking's bike was never loaded, and by the time the page reads it the session is closed, so the lazy reference throws, with a message in the same form as Hibernate's. The second booking's bike was loaded with the booking, as JOIN FETCH would do, so reading it after the session closes needs no query at all. The lesson is to load what the view needs before the transaction ends.
Indexes
Fetching less often is half of performance; the other half is making each query fast, and that is mostly about indexes. Without an index, a query that filters on a column must read every row of the table, a full scan. An index is a sorted structure, usually a B-tree, that lets the database jump to matching rows. Primary keys and UNIQUE columns are indexed automatically. Foreign keys are not, in PostgreSQL and SQLite, so booking.bike_id, which every join from bike to booking uses, needs an index of its own. Every database can show its query plan; in SQLite the command is EXPLAIN QUERY PLAN.
EXPLAIN QUERY PLAN
SELECT * FROM booking WHERE bike_tag = 'SG-0007-AR';What this code does
Without an index on bike_tag, SQLite reads the whole booking table and checks every row. With five rows that costs nothing, but the plan does not change with size, so at fifty thousand bookings it reads fifty thousand rows for every lookup.
| id | parent | notused | detail |
|---|---|---|---|
| 2 | 0 | 0 | SCAN booking |
EXPLAIN QUERY PLAN
SELECT * FROM booking WHERE bike_tag = 'SG-0007-AR';What this code does
After CREATE INDEX booking_bike_idx ON booking (bike_tag), the plan searches the index for the tag and reads only the matching rows. The index is chosen automatically; queries do not mention it.
| id | parent | notused | detail |
|---|---|---|---|
| 3 | 0 | 0 | SEARCH booking USING INDEX booking_bike_idx (bike_tag=?) |
Indexes are not free: each one takes space and slows every INSERT and UPDATE that changes its columns, so add them for the columns queries actually filter, join and sort on, and check the plan rather than guessing. The index itself is created in a migration, the subject of the next lesson, not by Hibernate. JPA's @Index annotation inside @Table only matters when the provider generates the schema.
import java.util.*;
public class Main {
record Booking(int id, int bikeId) {}
public static void main(String[] args) {
int rows = 50_000;
List<Booking> table = new ArrayList<>();
for (int i = 0; i < rows; i++) table.add(new Booking(i + 1, i % 40)); // 40 bikes
// full scan: every row is examined
int examined = 0, found = 0;
for (Booking b : table) { examined++; if (b.bikeId() == 7) found++; }
System.out.println("Full scan: examined " + examined + " rows, found " + found);
// index: a sorted map from bike id to the row positions for that bike
TreeMap<Integer, List<Integer>> index = new TreeMap<>();
for (int pos = 0; pos < table.size(); pos++) index.computeIfAbsent(table.get(pos).bikeId(), k -> new ArrayList<>()).add(pos);
List<Integer> positions = index.getOrDefault(7, List.of());
int steps = 32 - Integer.numberOfLeadingZeros(index.size()); // about log2 of the keys
System.out.println("Index: about " + steps + " steps to find the key, then " + positions.size() + " rows read");
System.out.println("First matching ids: " + positions.subList(0, 3).stream().map(p -> table.get(p).id()).toList());
}
}Full scan: examined 50000 rows, found 1250 Index: about 6 steps to find the key, then 1250 rows read First matching ids: [8, 48, 88]
What this code does
The scan looks at all 50,000 rows to find the 1,250 for bike 7. The index, a sorted map from key to row positions, reaches the key in about six comparisons for forty keys and then reads only the matching rows. A database B-tree works on the same principle, with pages on disk instead of map entries.
A performance checklist
Hamza's fix for the bookings page had four parts, and they make a good checklist for any JPA screen. Turn on SQL logging, or Hibernate's statistics with hibernate.generate_statistics, in development and count the queries per request; a number that grows with the data is an N+1 problem. Make every relationship LAZY and fetch per use case with JOIN FETCH, entity graphs or projections. Set a default batch fetch size as a safety net. And check the query plans of the remaining queries, adding indexes where they scan. After the changes, the page sent two queries and opened in under a tenth of a second, and Odile stopped mentioning it, which Ingrid said was the only performance metric that counts at a rental desk.
Try it yourself
Count the round trips
Write nPlusOne(int n) returning the queries for loading n bookings and then each booking's bike one at a time, and batched(int distinctBikes, int batchSize) returning 1 plus the number of IN queries needed, rounded up.
The program prints 21, 3 and 1.
public class Main {
static int nPlusOne(int n) { return 0; }
static int batched(int distinctBikes, int batchSize) { return 0; }
public static void main(String[] args) {
System.out.println(nPlusOne(20));
System.out.println(batched(6, 4));
System.out.println(batched(0, 4));
}
}Try it yourself
Load only once
Complete get in Lazy so the loader runs only on the first call; later calls return the stored value.
The program prints CARGO CARGO and then loads: 1.
import java.util.function.Supplier;
public class Main {
static int loads = 0;
static class Lazy<T> {
private final Supplier<T> loader;
private T value;
Lazy(Supplier<T> loader) { this.loader = loader; }
T get() {
return loader.get();
}
}
public static void main(String[] args) {
Lazy<String> kind = new Lazy<>(() -> { loads++; return "CARGO"; });
System.out.println(kind.get() + " " + kind.get());
System.out.println("loads: " + loads);
}
}Common mistakes
MistakeLeaving @ManyToOne at its EAGER default.
InsteadSet fetch = FetchType.LAZY and fetch per query.
MistakeFixing LazyInitializationException by making relationships EAGER.
InsteadFetch what the view needs with JOIN FETCH, an entity graph or a projection.
MistakeRelying on open session in view.
InsteadLoad the view's data inside the transaction and turn the setting off.
MistakePaging a query that JOIN FETCHes a collection.
InsteadPage the parents, then fetch children with batch fetching or a second query.
MistakeAssuming foreign keys are indexed.
InsteadCreate indexes on foreign key columns in a migration and check the query plan.
Summary and key terms
- Eager loading fetches related entities at once; lazy loading uses proxies or provider collections that load on first use.
- The JPA defaults are EAGER for @ManyToOne and @OneToOne and LAZY for collections, and the usual advice is to make everything lazy.
- Loading a list and then each element's relationship is the N+1 select problem, whose cost is round trips.
- JOIN FETCH, entity graphs, batch fetching and projections load what each use case needs; fetching collections with paging needs care.
- Touching an unloaded proxy after the persistence context closes throws LazyInitializationException, which is fixed by fetching inside the transaction, not by EAGER or open session in view.
- Indexes let queries find rows without full scans; primary keys are indexed automatically but foreign keys often are not, and EXPLAIN QUERY PLAN shows what a query does.
- Count queries per request in development to find problems early.
- Eager loading
- Fetching related entities at the same time as the entity that refers to them.
- Lazy loading
- Fetching related entities only when the program first uses them.
- Fetch type
- The LAZY or EAGER setting of a relationship.
- Proxy
- A stand-in object that knows an entity's id and loads the rest on first use.
- N+1 select problem
- One query for a list followed by one query per element.
- JOIN FETCH
- A JPQL join that loads the related entities in the same query.
- Entity graph
- A named set of relationships to load, passed to a find or query.
- Batch fetching
- Loading several lazy proxies of the same type in one IN query.
- LazyInitializationException
- Hibernate's exception for touching an unloaded proxy after its session has closed.
- Query plan
- The steps a database will take to run a query, such as a scan or an index search.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Which relationships are EAGER by default in JPA?
Show answer
@ManyToOne and @OneToOne.
How many queries does the N+1 pattern send for 20 bookings and their bikes?
Show answer
21.
Why does getId on a Hibernate proxy not cause a query?
Show answer
The proxy already holds the id.
What is wrong with fixing LazyInitializationException with EAGER?
Show answer
Every query then loads the relationship, whether or not it is needed.
What did EXPLAIN QUERY PLAN show before and after the index on bike_tag?
Show answer
A scan of booking before, and a search using the index after.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.