Kayavak / The Backend / BE·49 Persistence — JDBC, JPA, Hibernate and Spring Data / Deeper
Deeper · The Backend
Persistence — JDBC, JPA, Hibernate and Spring Data
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Programs model the world as objects with references; relational databases model it as tables of rows joined by values. Translating between them by hand (writing SQL, reading result sets column by column into objects) was the bulk of business code in the 1990s; JDBC (1997) standardised the database calls but not the translation. The mismatch got a name, the object–relational impedance mismatch, and tools to bridge it: Gavin King's Hibernate (2001) mapped classes to tables and loaded related objects automatically, and its design became the Java Persistence API (JPA, 2006). Spring Data added repositories generated from method names.
The bridge leaks. Ted Neward called object–relational mapping "the Vietnam of computer science" in 2006: easy at the start, ever more costly to finish. The classic symptoms (N+1 queries, surprise lazy loads, flushes at odd moments) come from hiding the database's cost model behind an object model. Modern practice uses ORMs for straightforward entities and plain SQL for reports and hot paths.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
An ORM keeps a unit of work: a set of objects it loaded and watches. Changes to them become SQL when it flushes, usually at commit. Every query you didn't write is one it decided to issue, so watch the SQL.
| Question | Answer |
|---|---|
| What is it? | Layers between Java objects and a relational database: JDBC, JdbcClient, JPA/Hibernate, Spring Data |
| Problem it solves | Reading and writing relational data from object-oriented code without hand-writing every mapping |
| Parts | Connection pool, transactions, entities and mappings, the persistence context, lazy loading, flush, queries |
| How it works | Load entities into the persistence context; track changes (dirty checking); write them at flush within the transaction |
| Assumptions | You know which queries your code causes and where the transaction ends |
| When they change | Touching a lazy collection in a loop issues one query per item (N+1); accessing it after the session closes throws |
| Where it fails | Bulk operations, complex reporting queries, and performance-critical paths |
| Related ideas | Relational model and indexes (Databases); transactions and isolation (Databases); connection pools and Little's law (ch41) |
What if?
What if you loop over 100 orders and read each one's customer?
Reason it through, then open
With a lazy association, the ORM runs one query for the orders and then one query per order for its customer: 101 queries where one join would do. It looks fine with 3 test rows and collapses with real data. Fetch joins, entity graphs or a batch-fetch size make the loading explicit.
What if the connection pool had 10 connections and requests held them for a second each?
Reason it through, then open
By Little's law (ch41), the pool can serve at most 10 requests per second; the eleventh waits. Holding a connection during slow work (a remote call inside a transaction) shrinks capacity further. Keep transactions short and do remote calls outside them.
Where the boundary is
| Aspect | ORM (JPA/Hibernate) | Plain SQL (JdbcClient) |
|---|---|---|
| Good for | Entity lifecycles: load, change, save | Reports, bulk updates, tuned hot queries |
| You control the SQL | Indirectly | Exactly |
| Hidden costs | Lazy loads, dirty checking, flushes | None beyond what you wrote |
| Risk | N+1 queries, surprising writes | Hand-mapping mistakes |
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Hibernate began in 2001; JPA standardised in 2006 | Historical | Project history; JSR 220 |
| "The Vietnam of computer science" | Historical | Ted Neward's 2006 essay |
| N+1 queries are the most common ORM performance problem | Model | Widely reported; not formally measured |
| Dirty checking writes changed entities at flush | Convention | JPA specification and Hibernate's behaviour |
Still open
How much abstraction over SQL is worth having is argued in every ecosystem: type-safe SQL builders (jOOQ), micro-ORMs and full ORMs each have committed users.
What would change your mind? If an ORM's generated SQL were always as good as hand-written SQL, the case for writing SQL would be convenience only. Logging the SQL usually shows where it isn't.
Recall
What causes the N+1 query problem?
Lazily loading an association for each item in a loop, one query per item.
What is the persistence context?
The set of entities an ORM has loaded and is tracking for changes within a unit of work.
Why keep remote calls out of database transactions?
They hold a connection (and locks) while waiting, shrinking the pool's capacity.
When is plain SQL better than an ORM?
Reports, bulk operations and performance-critical queries.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.