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 184.108.40.206 Fetching a Proxy reference 220.127.116.11 Natural identifier fetching 15.3.2 Query fetching 15.3.3 Fetching associations 18.104.22.168 FetchType.EAGER 22.214.171.124 FetchType.LAZY 126.96.36.199.1 The N+1 query problem 188.8.131.52.2 How to catch N+1 query problems during testing 184.108.40.206.3 LazyInitializationException 220.127.116.11.4 The Open Session in View Anti-Pattern 18.104.22.168.5 Temporary Session Lazy Loading Anti-Pattern 22.214.171.124 Associations and pagination 15.4 Query plan cache
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.
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
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
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.