High-Performance Java Persistence – Chapter 15 – Fetching
Imagine having a tool that can automatically detect JPA and Hibernate performance issues. Wouldn’t that be just awesome?
Well, Hypersistence Optimizer is that tool! And it works with Spring Boot, Spring Framework, Jakarta EE, Java EE, Quarkus, or Play Framework.
So, enjoy spending your time on the things you love rather than fixing performance issues in your production system on a Saturday night!
Part 2, Chapter 15
Every new chapter of my book is released right after it’s being completed, so the reader doesn’t have to wait for the whole part to be finished to get access to new material.
Table of content
This chapter explains how fetching works in Hibernate.
15. Fetching 15.1 DTO projection 15.1.1 DTO projection pagination 15.1.2 Native query DTO projection 15.2 Query fetch size 15.3 Fetching entities 15.3.1 Direct fetching 15.3.1.1 Fetching a Proxy reference 15.3.1.2 Natural identifier fetching 15.3.2 Query fetching 15.3.3 Fetching associations 15.3.3.1 FetchType.EAGER 15.3.3.2 FetchType.LAZY 15.3.3.2.1 The N+1 query problem 15.3.3.2.2 How to catch N+1 query problems during testing 15.3.3.2.3 LazyInitializationException 15.3.3.2.4 The Open Session in View Anti-Pattern 15.3.3.2.5 Temporary Session Lazy Loading Anti-Pattern 15.3.3.3 Associations and pagination 15.4 Query plan cache
Chapter summary
Fetching data has a great impact on data access layer performance, and fetching way too much data is one of the most common performance-related issues. In this chapter, I explain when you should use entity fetching and when to switch to DTO projections.
There is also a very interesting example that demonstrates why, sometimes, processing data in the database (Recursive CTE, Window Functions) can outperform application-level data processing.
The FetchType.EAGER
is often a good hint that an application might experience performance issues. But FetchType.LAZY
is not without problems either because it can lead to N+1 query problems or LazyInitializationException
.
Unfortunately, there are also some widespread anti-patterns such as
Open Session in View or Temporary Session Lazy Loading which cure the symptoms and do not address the root cause of a LazyInitializationException
.
A lesser-known configuration is the query plan cache, and you’ll also learn how you can tune it, and for which queries it makes more sense to save the pre-compilation phase.
Enjoy reading High-Performance Java Persistence!
P.S. There is also a Google Group dedicated to this book, so, if you’d like to join and discuss the book content, don’t hesitate to join in.
