Posts
read more
Avoiding ORM Traps and the N+1 Problem in Java
Introduction
An ORM like JPA/Hibernate makes database access feel like working with plain objects. That is exactly the problem: a single innocent-looking loop can fire hundreds of SQL queries, and you won’t see it until production slows to a crawl.
This post covers the ORM traps that bite Java teams most often — starting with the notorious N+1 query problem — and the concrete techniques that fix them.
The N+1 Problem
Consider loading authors and printing their books: