Hibernate المتقدم
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- خطط الجلب (Fetch Plans)
- الدفعات (batching)
- ذاكرة التخزين المؤقت من المستوى الأول والثاني
- حالات الكيان (Entity States)
تعلّم أولًا
3 ساعة تدريب (تقدير)
مخرجات التعلم
- Design fetch plans per use case with entity graphs and DTO projections.
- Configure JDBC batching and explain why identity generation disables insert batching.
- Use the second-level cache and query cache appropriately, or decide against them.
- Handle detached entities and merging correctly.
الدروس
المختبر والتمارين
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)
| ID | Outcome | Task |
|---|---|---|
| MID-021-EX01 | 1 | Fix an N+1 on Order.items with an entity graph and verify the statement count in a test. |
| MID-021-EX02 | 1 | Write a DTO projection for the order list screen and compare the SQL with loading entities. |
| MID-021-EX03 | 2 | Configure batching and show the effect on 10 000 updates. |
| MID-021-EX04 | 2 | Change an IDENTITY id to a sequence and show insert batching starting to work. |
| MID-021-EX05 | 3 | Decide for five entities whether to use the second-level cache and justify each. |
| MID-021-EX06 | 4 | Explain what happens with persist, merge, and save on a detached entity, and what each returns. |
| MID-021-EX07 | 4 | Fix a bug where code modifies the object passed to merge expecting changes to be saved. |
أظهر الحلول
- EX01
@EntityGraph(attributePaths = "items")on the repository method; assertgetPrepareStatementCount()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 matchingallocationSizeand a sequenceINCREMENT BYof 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
persiston a detached entity throws;mergereturns a managed copy;savecallsmergefor 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 "
saveAllwithout 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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).