High-Performance Java Persistence – Chapter 15 – Fetching

(Last Updated On: February 27, 2019)
Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

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  Fetching a Proxy reference Natural identifier fetching        
15.3.2 Query fetching           
15.3.3 Fetching associations  FetchType.EAGER   FetchType.LAZY The N+1 query problem How to catch N+1 query problems during testing LazyInitializationException The Open Session in View Anti-Pattern Temporary Session Lazy Loading Anti-Pattern 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 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.

Download free ebook sample

Newsletter logo
10 000 readers have found this blog worth following!

If you subscribe to my newsletter, you'll get:
  • A free sample of my Video Course about running Integration tests at warp-speed using Docker and tmpfs
  • 3 chapters from my book, High-Performance Java Persistence,
  • a 10% discount coupon for my book.

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.

Want to run your data access layer at warp speed?