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

Hibernate المتقدم

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

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

نظرة عامة

الموضوعات

  • خطط الجلب (Fetch Plans)
  • الدفعات (batching)
  • ذاكرة التخزين المؤقت من المستوى الأول والثاني
  • حالات الكيان (Entity States)

تعلّم أولًا

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

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

  1. Design fetch plans per use case with entity graphs and DTO projections.
  2. Configure JDBC batching and explain why identity generation disables insert batching.
  3. Use the second-level cache and query cache appropriately, or decide against them.
  4. Handle detached entities and merging correctly.

الدروس

  1. 01خطط الجلب حسب حالة الاستخدام
  2. 02إدراج وتحديث البيانات على دفعات
  3. 03ذاكرة المستوى الثاني وذاكرة الاستعلامات المؤقتة
  4. 04العودة إلى حالات الكيان: merge مقابل persist
  5. 05إحصائيات Hibernate
  6. 06عرض عملي: تحديثات جماعية أسرع 10 مرات بالدفعات

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

Lab

MID-021-LAB1 · Batch job tuning

Outcomes: 1–4 · Time: ~3 h Make Shop's nightly price-update job (100 000 products) 10× faster without leaving JPA: fetch plan, JDBC batching, periodic flush/clear, and (if useful) a cache decision for the category lookup. Acceptance: statement counts and timings before and after, recorded with Hibernate statistics.

Exercises (assessment: tuning exercise)

IDOutcomeTask
MID-021-EX011Fix an N+1 on Order.items with an entity graph and verify the statement count in a test.
MID-021-EX021Write a DTO projection for the order list screen and compare the SQL with loading entities.
MID-021-EX032Configure batching and show the effect on 10 000 updates.
MID-021-EX042Change an IDENTITY id to a sequence and show insert batching starting to work.
MID-021-EX053Decide for five entities whether to use the second-level cache and justify each.
MID-021-EX064Explain what happens with persist, merge, and save on a detached entity, and what each returns.
MID-021-EX074Fix a bug where code modifies the object passed to merge expecting changes to be saved.
أظهر الحلول
  • EX01 @EntityGraph(attributePaths = "items") on the repository method; assert getPrepareStatementCount() equals 1.
  • EX02 select new ...OrderSummary(...): fewer columns, no entity instances, no lazy-loading risk.
  • EX03 batch_size=50, order_updates=true: the statement execution count drops from 10 000 to about 200 batches (measured values vary).
  • EX04 @GeneratedValue(strategy = SEQUENCE) with matching allocationSize and a sequence INCREMENT BY of the same size; Hibernate logs batched inserts.
  • EX05 Cache read-mostly reference data (category, country); avoid stock, orders, and anything written often. Check the reasons given.
  • EX06 persist on a detached entity throws; merge returns a managed copy; save calls merge for entities with an id and returns the saved instance.
  • EX07 Use the returned managed instance: var managed = em.merge(x); managed.setName(...); the original remains detached.
  • The "saveAll without batching" mistake is shown in EX03 and the lab; caching mutable entities in EX05.

الاختبار

Outcomes: O1 fetch plans · O2 JDBC batching · O3 caches · O4 entity states and merge.

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

  1. 1The default recommended fetch type for associations:O1
  2. 2An entity graph or JOIN FETCH mainly prevents:O1
  3. 3For a read-only list screen the cheapest option is usually:O1
  4. 4Which id strategy disables JDBC insert batching?O2
  5. 5Why flush and clear in a bulk job?O2
  6. 6hibernate.jdbc.batch_size controls:O2
  7. 7A good second-level cache candidate:O3
  8. 8The query cache is invalidated when:O3
  9. 9merge returns:O4
  10. 10Preferred approach to updating an entity from a DTO:O4

الجزء العملي

Tuning exercise: given a slow job with N+1 loads, IDENTITY ids, and no batching, produce a tuned version with statement counts. Pass: quiz 8/10, three tuning changes made with measured effect, and the lab's before/after figures.

أخطاء شائعة

Caching mutable, frequently written entities; saveAll without batching configured.

التقييم

Tuning exercise.

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

Choosing persistence tools (MID-022).

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