العودة إلى حالات الكيان: merge مقابل persist
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
handle detached entities correctly.
الموضوعات المخطط لها
- States: transient (new), managed (in the persistence context), detached (loaded earlier, session closed), removed.
persistmakes a new entity managed;mergecopies 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
savecallspersistfor new entities andmergeotherwise;mergeof an unknown id may do an extraSELECT. - Prefer loading the entity inside the transaction and changing it (dirty checking) over merging DTO-built entities.
تفاصيل المخطط المبدئي
عرض عملي
Product managed = em.merge(detached); // use 'managed', not 'detached'اختبر فهمك
What does merge return?
أظهر الإجابة
The managed copy; the passed object stays detached.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.