انتقل إلى المحتوى
أمين أبوزيد
درس مخطط لهMID-021-L04

العودة إلى حالات الكيان: merge مقابل persist

  • الدرس 4 من 6
  • Mid-level
في هذا الدرس 4 موضوعات

هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.

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

في هذا الدرس

handle detached entities correctly.

الموضوعات المخطط لها

  • States: transient (new), managed (in the persistence context), detached (loaded earlier, session closed), removed.
  • persist makes a new entity managed; merge copies the state of a detached entity onto a managed copy and returns that copy.
  • Use the returned object from merge; the argument stays detached.
  • Spring Data save calls persist for new entities and merge otherwise; merge of an unknown id may do an extra SELECT.
  • Prefer loading the entity inside the transaction and changing it (dirty checking) over merging DTO-built entities.
تفاصيل المخطط المبدئي

عرض عملي

java
Product managed = em.merge(detached);   // use 'managed', not 'detached'

اختبر فهمك

What does merge return?

أظهر الإجابة

The managed copy; the passed object stays detached.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

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

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

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