Skip to content
Ameen Abozaid
JNR-027

ORM fundamentals

  • 4 Lessons
  • 1 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Object-relational impedance mismatch
  • persistence context

Learn first

1 h of practice (estimate)

Learning outcomes

  1. Explain the object-relational impedance mismatch (identity, inheritance, associations, granularity).
  2. Describe what an ORM automates and what it costs.
  3. Explain the persistence context as an identity map and unit of work.

Lessons

  1. 01Objects vs tables
  2. 02What an ORM does
  3. 03The persistence context
  4. 04Demo: watching the persistence context with SQL logging

Lab and exercises

Lab

JNR-027-LAB1 · Mismatch exercise

Outcomes: 1–3 · Time: ~1 h

  1. Open the Spring JDBC ProductRepository from JNR-026.
  2. List every piece of mapping code: SQL strings, row mappers, key handling, null handling, type conversions, joins assembled by hand.
  3. For each item, predict whether an ORM would remove it, change it, or leave it.
  4. For each, name one risk the ORM would add (hidden query, lazy loading, dirty checking surprise).
  5. 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)

IDOutcomeTask
JNR-027-EX011Name the four mismatches and give a one-line Shop example of each.
JNR-027-EX021Explain why Order with List<OrderLine> has no direct table equivalent.
JNR-027-EX032List three things an ORM automates and three costs it introduces.
JNR-027-EX042Explain why "we use an ORM so we do not need SQL" is wrong.
JNR-027-EX053Predict the number of selects for: find(1), find(1), find(2) in one transaction.
JNR-027-EX063Explain why changing a loaded entity without calling save can still update the row.
Show solutions
  • EX01 Identity: two Product objects with the same id; inheritance: DigitalProduct extends Product; associations: Order-OrderLine; granularity: Money as 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.

  1. 1The object-relational impedance mismatch is:O1
  2. 2Which is NOT one of the classic mismatches?O1
  3. 3A Money value object stored as two columns shows a mismatch of:O1
  4. 4An ORM automates:O2
  5. 5A real cost of an ORM is:O2
  6. 6JPA is:O2
  7. 7With an ORM you still need SQL knowledge to:O2
  8. 8The persistence context works as:O3
  9. 9find(1) twice in one transaction issues:O3
  10. 10Flush means:O3

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).

Type to search levels, phases, modules and lessons.