From de5cdf0bf89c613c4cfbdc6e68073d670018ed88 Mon Sep 17 00:00:00 2001 From: asmhatre Date: Sat, 25 Jul 2026 05:36:25 +0000 Subject: [PATCH] Add AuthorRepository.java --- .../sdjpa4demo/repo/AuthorRepository.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/com/ankurm/sdjpa4demo/repo/AuthorRepository.java diff --git a/src/main/java/com/ankurm/sdjpa4demo/repo/AuthorRepository.java b/src/main/java/com/ankurm/sdjpa4demo/repo/AuthorRepository.java new file mode 100644 index 0000000..506f89d --- /dev/null +++ b/src/main/java/com/ankurm/sdjpa4demo/repo/AuthorRepository.java @@ -0,0 +1,45 @@ +package com.ankurm.sdjpa4demo.repo; + +import com.ankurm.sdjpa4demo.domain.Author; +import org.jspecify.annotations.Nullable; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +public interface AuthorRepository + extends JpaRepository, JpaSpecificationExecutor { + + // (1) Optional single result -> empty when not found (never throws for "not found"). + Optional findByName(String name); + + // (2) Non-null single result. In a @NullMarked package this is the DEFAULT. + // Missing row -> EmptyResultDataAccessException. Null argument -> IllegalArgumentException. + Author getByName(String name); + + // (3) Explicitly @Nullable single result -> returns null when not found, + // and tolerates a null argument. + @Nullable + Author findByCountry(@Nullable String country); + + // (4) Derived query used to demonstrate the Criteria -> JPQL shift (watch the SQL log). + List findByNameStartingWithOrderByNameAsc(String prefix); + + // (5) Sort with explicit NULLS precedence. Consistent for derived queries in SD JPA 4. + List findByNameStartingWith(String prefix, Sort sort); + + // (6) New JPA 3.2 / Hibernate 7 JPQL features: || concatenation + replace() function. + @Query("select replace(a.name, ' ', '_') || ':' || coalesce(a.country, 'N/A') from Author a where a.id = :id") + String badgeFor(@Param("id") Long id); + + // (7) Derived DELETE returning the affected count. + @Modifying + @Transactional + long deleteByCountry(@Nullable String country); +}