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

Spring Data JPA

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

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

نظرة عامة

الموضوعات

  • المستودعات (Repositories)
  • الاستعلامات المشتقة (Derived queries)
  • JPQL
  • الاستعلامات الأصلية (Native queries)

تعلّم أولًا

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

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

  1. Define repositories extending JpaRepository and explain the generated implementation.
  2. Write derived queries and recognize when a name becomes unreadable.
  3. Write JPQL and native queries with @Query and parameters.
  4. Return projections (interface or record) for read-only views and use Pageable.

الدروس

  1. 01واجهات المستودعات (Repository interfaces)
  2. 02الاستعلامات المشتقة (Derived queries)
  3. 03JPQL مع @Query
  4. 04الاستعلامات الأصلية (Native queries)
  5. 05الإسقاطات (Projections) والترقيم
  6. 06عرض عملي: ترحيل الكتالوج إلى Spring Data JPA
  7. 07عرض عملي: الإسقاطات لقوائم مرقّمة

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

Lab

JNR-029-LAB1 · Catalog on JPA

Outcomes: 1–4 · Time: ~3 h

  1. Move products and categories to Spring Data JPA repositories.
  2. Add findBySku (derived) and a paged search by name, category, and price range (@Query).
  3. Return a ProductListItem projection for the listing; map to the API response in the service.
  4. Keep all existing endpoints and JSON unchanged.
  5. Run the existing API tests plus a repository test for the search.

Acceptance: API contract unchanged; tests pass.

Exercises (assessment: implement three queries from requirements)

IDOutcomeTask
JNR-029-EX011Declare CustomerRepository and use findById, save, count; explain who implements it.
JNR-029-EX022Write derived queries: products by status, products with name containing text (ignore case).
JNR-029-EX032Rewrite a 60-character derived method as a @Query.
JNR-029-EX043Write a JPQL query with named parameters: orders of a customer between two dates.
JNR-029-EX053Write a native query counting products per category and explain why native was chosen.
JNR-029-EX064Return a record projection and a Page sorted by price descending.
أظهر الحلول
  • EX01 interface CustomerRepository extends JpaRepository<Customer, Long> {}; Spring Data creates a proxy implementation at startup.
  • EX02 List<Product> findByStatus(ProductStatus s); and List<Product> findByNameContainingIgnoreCase(String text);
  • EX03 @Query("select p from Product p where p.category.id = :cid and p.price.amount <= :max order by p.name") List<Product> findAffordable(@Param("cid") Long cid, @Param("max") BigDecimal max);
  • EX04 select o from Order o where o.customer.id = :cid and o.createdAt between :from and :to.
  • EX05 select category_id, count(*) from product group by category_id with nativeQuery = true; chosen for a reporting aggregate returned as a projection, not because it is the default.
  • EX06 Page<ProductListItem> list(Pageable p); called with PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "price.amount")) (check the property path against your mapping).
  • Assessment covers EX02, EX04, and EX06 (three queries from requirements).

الاختبار

Outcomes: O1 repositories · O2 derived queries · O3 JPQL and native queries · O4 projections and paging.

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

  1. 1Who creates the implementation of a JpaRepository interface?O1
  2. 2findById returns:O1
  3. 3findByNameContainingIgnoreCase(String) produces:O2
  4. 4A derived name that is 60 characters long should be:O2
  5. 5JPQL queries refer to:O3
  6. 6Query input must be passed:O3
  7. 7Native queries are best used:O3
  8. 8A projection is:O4
  9. 9Slice differs from Page by:O4
  10. 10PageRequest.of(0, 20) means:O4

الجزء العملي

Six exercises JNR-029-EX01 to EX06 plus the lab; the assessment is implementing three queries from requirements (EX02, EX04, EX06). Pass: all three assessment queries correct, at least five of six exercises correct, and lab tests green.

أخطاء شائعة

60-character derived method names; native queries by default; returning entities to controllers.

التقييم

Implement three queries from requirements.

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

Daily work; advanced Hibernate in MID-021.

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