High-Performance Java Persistence – Chapter 15 – Fetching

Are you struggling with performance issues in your Spring, Jakarta EE, or Java EE application?

Imagine having a tool that could automatically detect performance issues in your JPA and Hibernate data access layer long before pushing a problematic change into production!

With the widespread adoption of AI agents generating code in a heartbeat, having such a tool that can watch your back and prevent performance issues during development, long before they affect production systems, can save your company a lot of money and make you a hero!

Hypersistence Optimizer is that tool, and it works with Spring Boot, Spring Framework, Jakarta EE, Java EE, Quarkus, Micronaut, or Play Framework.

So, rather than allowing performance issues to annoy your customers, you are better off preventing those issues using Hypersistence Optimizer and enjoying spending your time on the things that you love!

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.

Transactions and Concurrency Control eBook

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.