واحد إلى متعدد والطرف المالك
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
map the collection side and know who owns the relationship.
الموضوعات المخطط لها
@OneToMany(mappedBy = "order")is the inverse side: it shows the children but does not write the foreign key.- The side with
@JoinColumn(the many-to-one) is the owning side; only its value is saved. - Keep both sides in sync with helper methods (
addLine,removeLine). - Initialize collections (
new ArrayList<>()) and expose them carefully. - Forgetting to set the owning side saves a
nullforeign key.
تفاصيل المخطط المبدئي
عرض عملي
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
private List<OrderLine> lines = new ArrayList<>();
public void addLine(OrderLine line) {
lines.add(line);
line.setOrder(this); // owning side
}اختبر فهمك
Which side's value is written to the foreign key column?
أظهر الإجابة
The owning side, the one without mappedBy.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.