Spring Data JPA
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- المستودعات (Repositories)
- الاستعلامات المشتقة (Derived queries)
- JPQL
- الاستعلامات الأصلية (Native queries)
تعلّم أولًا
3 ساعة تدريب (تقدير)
مخرجات التعلم
- Define repositories extending
JpaRepositoryand explain the generated implementation. - Write derived queries and recognize when a name becomes unreadable.
- Write JPQL and native queries with
@Queryand parameters. - Return projections (interface or record) for read-only views and use
Pageable.
الدروس
المختبر والتمارين
Lab
JNR-029-LAB1 · Catalog on JPA
Outcomes: 1–4 · Time: ~3 h
- Move products and categories to Spring Data JPA repositories.
- Add
findBySku(derived) and a paged search by name, category, and price range (@Query). - Return a
ProductListItemprojection for the listing; map to the API response in the service. - Keep all existing endpoints and JSON unchanged.
- 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)
| ID | Outcome | Task |
|---|---|---|
| JNR-029-EX01 | 1 | Declare CustomerRepository and use findById, save, count; explain who implements it. |
| JNR-029-EX02 | 2 | Write derived queries: products by status, products with name containing text (ignore case). |
| JNR-029-EX03 | 2 | Rewrite a 60-character derived method as a @Query. |
| JNR-029-EX04 | 3 | Write a JPQL query with named parameters: orders of a customer between two dates. |
| JNR-029-EX05 | 3 | Write a native query counting products per category and explain why native was chosen. |
| JNR-029-EX06 | 4 | Return 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);andList<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_idwithnativeQuery = true; chosen for a reporting aggregate returned as a projection, not because it is the default. - EX06
Page<ProductListItem> list(Pageable p);called withPageRequest.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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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.