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

JPA وHibernate

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

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

نظرة عامة

الموضوعات

  • الكيانات (Entities)
  • المعرّفات (Identifiers)
  • العلاقات
  • دورة حياة الكيان
  • الفحص الذكي للتغييرات (Dirty checking)

تعلّم أولًا

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

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

  1. Map entities with @Entity, @Id, generated identifiers, columns, enums, and embedded value objects.
  2. Describe entity states (transient, managed, detached, removed) and transitions.
  3. Explain dirty checking and flush timing, and predict when SQL is issued.
  4. Implement equals/hashCode safely for entities.
  5. Enable SQL logging and read the generated statements.

الدروس

  1. 01ربط كيان بجدول (Mapping an entity)
  2. 02المعرّفات واستراتيجيات توليدها
  3. 03Embeddables وكائنات القيمة (Value objects)
  4. 04دورة حياة الكيان
  5. 05الفحص الذكي للتغييرات (Dirty checking) والـ Flush
  6. 06تساوي الكيانات
  7. 07عرض عملي: ربط كيانات Shop وتوقّع SQL الناتج
  8. 08عرض عملي: حالات الكيان في المُنقّح (Debugger)

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

Lab

JNR-028-LAB1 · Entities for Shop

Outcomes: 1–5 · Time: ~4 h

  1. Map Category and Product (with Money as an embeddable and a status enum) onto the existing schema.sql; keep ddl-auto=none.
  2. Enable SQL logging.
  3. In a transactional test run: create a product, update its price, read it again in a new transaction.
  4. For each statement in the log, write one sentence on why it ran at that moment.
  5. Write a safe equals/hashCode for Product and a test that adds it to a HashSet before and after saving.

Acceptance: the written explanation matches the SQL log statement by statement.

Exercises (assessment: "predict the SQL")

IDOutcomeTask
JNR-028-EX011Add @Entity, @Id, and column mappings to a Customer class with a status enum.
JNR-028-EX021Turn Address(street, city, zip) into an embeddable used by Customer.
JNR-028-EX032Label each object as transient, managed, detached, or removed in a given 10-line code listing.
JNR-028-EX043Predict the SQL (statements and order) for: load a product, change the price, call a JPQL query, commit.
JNR-028-EX054Fix an entity with Lombok @Data by writing correct equals/hashCode.
JNR-028-EX065Turn on SQL logging with parameter values and explain one logged insert.
JNR-028-EX071, 3Explain why IDENTITY disables insert batching and what to use instead.
أظهر الحلول
  • EX01 @Entity @Table(name="customer"), @Id @GeneratedValue private Long id;, @Enumerated(EnumType.STRING) private CustomerStatus status;, protected no-arg constructor.
  • EX02 @Embeddable record Address(...) and @Embedded private Address address; in Customer.
  • EX03 new objects are transient; objects loaded or persisted inside the open transaction are managed; those from a closed context are detached; those after remove are removed.
  • EX04 select ... from product where id=?; then (auto flush before the query) update product set price=? where id=?; then the query select; commit has nothing left to flush.
  • EX05 Remove @Data; use equals on non-null id with getClass().hashCode(), or a stable business key such as sku.
  • EX06 logging.level.org.hibernate.SQL=DEBUG and logging.level.org.hibernate.orm.jdbc.bind=TRACE (verify property names for your Hibernate version).
  • EX07 With IDENTITY the id exists only after the insert, so Hibernate must send each insert immediately; SEQUENCE lets it fetch ids first and batch inserts.

الاختبار

Outcomes: O1 entity mapping · O2 entity states · O3 dirty checking and flush · O4 equals/hashCode · O5 SQL logging.

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

  1. 1The safe way to store an enum is:O1
  2. 2An @Embeddable is stored:O1
  3. 3GenerationType.IDENTITY has the drawback of:O1
  4. 4Entities should be returned directly from controllers:O1
  5. 5An object created with new Product() is:O2
  6. 6After the transaction ends, a loaded entity is:O2
  7. 7Dirty checking means Hibernate:O3
  8. 8p.setName("X") on a managed entity inside a transaction:O3
  9. 9Why is Lombok @Data risky on entities?O4
  10. 10To see generated SQL you:O5

الجزء العملي

Seven exercises JNR-028-EX01 to EX07 plus the lab; the assessment is the "predict the SQL" exercise (EX04). Pass: EX04 correct, at least five of seven exercises correct, and the lab explanation matches the log.

أخطاء شائعة

Entities as API DTOs; GenerationType.IDENTITY without understanding batching impact; Lombok @Data on entities.

التقييم

"predict the SQL" exercise.

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

The default persistence stack for Spring services.

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