High-Performance Java Persistence – Chapter 15 – Fetching

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 which 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 precompilation 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.

Enter your email address to follow this blog and receive notifications of new posts by email.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s