انتقل إلى المحتوى
أمين أبوزيد
JNR-030

علاقات الكيانات

  • 8 درس
  • 4 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • واحد إلى واحد (One-to-one)
  • واحد إلى متعدد (One-to-many)
  • متعدد إلى واحد (Many-to-one)
  • متعدد إلى متعدد (Many-to-many)
  • التسلسل (Cascade)
  • حذف اليتامى (Orphan removal)

تعلّم أولًا

4 ساعة تدريب (تقدير)

مخرجات التعلم

  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.

الدروس

  1. 01متعدد إلى واحد: العلاقة الافتراضية
  2. 02واحد إلى متعدد والطرف المالك
  3. 03واحد إلى واحد
  4. 04متعدد إلى متعدد وكيانات الربط
  5. 05التسلسل (Cascades) وحذف اليتامى
  6. 06أحادية الاتجاه افتراضيًا
  7. 07عرض عملي: ربط Order وOrderLine كتجميع (Aggregate)
  8. 08عرض عملي: مفاجآت التسلسل وحذف اليتامى

المختبر والتمارين

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.
أظهر الحلول
  • 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.

الاختبار

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

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  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

الجزء العملي

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.

أخطاء شائعة

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

التقييم

Map a provided ER diagram.

الصلة بالمسار الوظيفي

Aggregates in DDD (MID-060).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.