12 Fetch plans, strategies, and profiles


This chapter covers

  • Working with lazy and eager loading
  • Applying fetch plans, strategies, and profiles
  • Optimizing SQL execution

In this chapter, we’ll explore Hibernate’s solution for the fundamental ORM problem of navigation, as introduced in section 1.2.5: the difference in how you access data in Java code and within a relational database. We’ll demonstrate how to retrieve data from the database and how to optimize this loading.

Hibernate provides the following ways to get data out of the database and into memory:

12.1 Lazy and eager loading

12.1.1 Understanding entity proxies

12.1.2 Lazy persistent collections

12.1.3 Eager loading of associations and collections

12.2 Selecting a fetch strategy

12.2.1 The n+1 selects problem

12.2.2 The Cartesian product problem

12.2.3 Prefetching data in batches

12.2.4 Prefetching collections with subselects

12.2.5 Eager fetching with multiple SELECTs

12.2.6 Dynamic eager fetching

12.3 Using fetch profiles

12.3.1 Declaring Hibernate fetch profiles

12.3.2 Working with entity graphs