Entity relationships
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
- Map one-to-one, one-to-many, many-to-one, and many-to-many relationships.
- Identify the owning side and keep bidirectional associations in sync with helper methods.
- Choose cascade types and orphan removal for aggregates, and explain the risks.
- Replace a many-to-many with an explicit join entity when the link has data.
Lessons
Lab and exercises
Lab
JNR-030-LAB1 · Orders
Outcomes: 1–4 · Time: ~4 h
- Map
OrderwithOrderLines (cascade = ALL,orphanRemoval = true) and helper methods. - Map
Ordermany-to-one toCustomerandOrderLinemany-to-one toProduct, both lazy. - Write tests: save an order with two lines; remove one line; delete the order.
- Prove that deleting an order does not delete products or customers.
- Add
unitPricetoOrderLineto 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)
| ID | Outcome | Task |
|---|---|---|
| JNR-030-EX01 | 1 | Map Product to Category many-to-one with lazy fetch and a join column. |
| JNR-030-EX02 | 1 | Map a one-to-one Customer and CustomerProfile with a unique foreign key. |
| JNR-030-EX03 | 2 | In a given bidirectional mapping, mark the owning side and write the missing helper method. |
| JNR-030-EX04 | 3 | Choose cascade settings for Order->OrderLine, Order->Customer, OrderLine->Product; justify each. |
| JNR-030-EX05 | 4 | Replace @ManyToMany Student-Course with an Enrollment entity that stores a grade. |
| JNR-030-EX06 | 1-4 | Map 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);
mappedBymarks the inverse. Helper:addX(x) { xs.add(x); x.setParent(this); }. - EX04
Order->OrderLine:ALLplus orphan removal (owned children);Order->Customer: none (independent lifecycle);OrderLine->Product: none. - EX05
Enrollment(id, student ManyToOne, course ManyToOne, grade);StudentandCourseeach 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.
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).