ORM fundamentals
On this page 8 topics
Overview
Topics
- Object-relational impedance mismatch
- persistence context
Learn first
1 h of practice (estimate)
Learning outcomes
- Explain the object-relational impedance mismatch (identity, inheritance, associations, granularity).
- Describe what an ORM automates and what it costs.
- Explain the persistence context as an identity map and unit of work.
Lessons
Lab and exercises
Lab
JNR-027-LAB1 · Mismatch exercise
Outcomes: 1–3 · Time: ~1 h
- Open the Spring JDBC
ProductRepositoryfrom JNR-026. - List every piece of mapping code: SQL strings, row mappers, key handling, null handling, type conversions, joins assembled by hand.
- For each item, predict whether an ORM would remove it, change it, or leave it.
- For each, name one risk the ORM would add (hidden query, lazy loading, dirty checking surprise).
- Tag each item with the mismatch it belongs to (identity, inheritance, associations, granularity).
Acceptance: a reasoned list with a prediction and a risk for every mapping item.
Exercises (assessment: quiz)
| ID | Outcome | Task |
|---|---|---|
| JNR-027-EX01 | 1 | Name the four mismatches and give a one-line Shop example of each. |
| JNR-027-EX02 | 1 | Explain why Order with List<OrderLine> has no direct table equivalent. |
| JNR-027-EX03 | 2 | List three things an ORM automates and three costs it introduces. |
| JNR-027-EX04 | 2 | Explain why "we use an ORM so we do not need SQL" is wrong. |
| JNR-027-EX05 | 3 | Predict the number of selects for: find(1), find(1), find(2) in one transaction. |
| JNR-027-EX06 | 3 | Explain why changing a loaded entity without calling save can still update the row. |
Show solutions
- EX01 Identity: two
Productobjects with the same id; inheritance:DigitalProduct extends Product; associations:Order-OrderLine; granularity:Moneyas amount and currency columns. - EX02 A list lives inside the object; in the database each line is a row in another table pointing back with a foreign key.
- EX03 Automates: generated SQL, row mapping, change tracking. Costs: hidden queries, N+1 risk, learning curve and debugging effort.
- EX04 You must design the schema, read generated SQL, write queries for reports, and diagnose slow statements.
- EX05 Two selects; the second
find(1)comes from the identity map. - EX06 The persistence context tracks managed entities and writes changes at flush (dirty checking: unit of work).
- Outcome coverage: the quiz also tests all three outcomes.
Quiz
Outcomes: O1 impedance mismatch · O2 what an ORM does and costs · O3 persistence context.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Six exercises JNR-027-EX01 to EX06 plus the lab; the assessment is this quiz. Pass: at least 8 of 10 quiz questions and a reasoned lab list.
Common mistakes
Believing an ORM removes the need to know SQL.
Assessment
Quiz.
Career relevance
Basis for diagnosing ORM issues (JNR-032, MID-021).