Skip to content
Ameen Abozaid
JNR-030

Entity relationships

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

Overview

Topics

  • One-to-one
  • one-to-many
  • many-to-one
  • many-to-many
  • cascade
  • orphan removal

Learn first

4 h of practice (estimate)

Learning outcomes

  1. Map one-to-one, one-to-many, many-to-one, and many-to-many relationships.
  2. Identify the owning side and keep bidirectional associations in sync with helper methods.
  3. Choose cascade types and orphan removal for aggregates, and explain the risks.
  4. Replace a many-to-many with an explicit join entity when the link has data.

Lessons

  1. 01Many-to-one: the default relationship
  2. 02One-to-many and the owning side
  3. 03One-to-one
  4. 04Many-to-many and join entities
  5. 05Cascades and orphan removal
  6. 06Unidirectional by default
  7. 07Demo: mapping Order and OrderLine as an aggregate
  8. 08Demo: cascade and orphan-removal surprises

Lab and exercises

Lab

JNR-030-LAB1 · Orders

Outcomes: 1–4 · Time: ~4 h

  1. Map Order with OrderLines (cascade = ALL, orphanRemoval = true) and helper methods.
  2. Map Order many-to-one to Customer and OrderLine many-to-one to Product, both lazy.
  3. Write tests: save an order with two lines; remove one line; delete the order.
  4. Prove that deleting an order does not delete products or customers.
  5. Add unitPrice to OrderLine to show why it is a join entity, not @ManyToMany.

Acceptance: removing a line from the collection deletes it; no cascade from order to product.

Exercises (assessment: map a provided ER diagram)

IDOutcomeTask
JNR-030-EX011Map Product to Category many-to-one with lazy fetch and a join column.
JNR-030-EX021Map a one-to-one Customer and CustomerProfile with a unique foreign key.
JNR-030-EX032In a given bidirectional mapping, mark the owning side and write the missing helper method.
JNR-030-EX043Choose cascade settings for Order->OrderLine, Order->Customer, OrderLine->Product; justify each.
JNR-030-EX054Replace @ManyToMany Student-Course with an Enrollment entity that stores a grade.
JNR-030-EX061-4Map a provided ER diagram (library: Author, Book, Loan, Member) and list your choices.
Show solutions
  • EX01 @ManyToOne(fetch = LAZY, optional = false) @JoinColumn(name = "category_id") private Category category;
  • EX02 @OneToOne(fetch = LAZY) @JoinColumn(name = "customer_id", unique = true) private Customer customer; in the profile.
  • EX03 The owning side holds the foreign key (the many-to-one); mappedBy marks the inverse. Helper: addX(x) { xs.add(x); x.setParent(this); }.
  • EX04 Order->OrderLine: ALL plus orphan removal (owned children); Order->Customer: none (independent lifecycle); OrderLine->Product: none.
  • EX05 Enrollment(id, student ManyToOne, course ManyToOne, grade); Student and Course each hold @OneToMany(mappedBy).
  • EX06 Loan joins Member and Book with dates, so it is an entity; Author-Book is many-to-many (join entity if it has data); keep relationships unidirectional unless needed.

Quiz

Outcomes: O1 relationship types · O2 owning side and sync · O3 cascade and orphan removal · O4 join entities.

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 foreign key column lives on the:O1
  2. 2The default fetch type of @ManyToOne is:O1
  3. 3Recommended fetch type for @ManyToOne:O1
  4. 4The owning side is the one:O2
  5. 5If you add to the list but do not set the owning side:O2
  6. 6Helper methods like addLine exist to:O2
  7. 7orphanRemoval = true means:O3
  8. 8Should OrderLine cascade REMOVE to Product?O3
  9. 9@ManyToMany is a problem when the link:O4
  10. 10OrderLine with quantity and price is best modeled as:O4

Practical part

Six exercises JNR-030-EX01 to EX06 plus the lab; the assessment is mapping a provided ER diagram (EX06). Pass: the ER mapping has correct owning sides and cascades, at least five of six exercises correct, lab tests green.

Common mistakes

CascadeType.ALL everywhere; bidirectional mappings that are never synchronized; @ManyToMany with extra columns.

Assessment

Map a provided ER diagram.

Career relevance

Aggregates in DDD (MID-060).

Type to search levels, phases, modules and lessons.