Add AuthorRepository.java
This commit is contained in:
@@ -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<Author, Long>, JpaSpecificationExecutor<Author> {
|
||||
|
||||
// (1) Optional single result -> empty when not found (never throws for "not found").
|
||||
Optional<Author> 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<Author> findByNameStartingWithOrderByNameAsc(String prefix);
|
||||
|
||||
// (5) Sort with explicit NULLS precedence. Consistent for derived queries in SD JPA 4.
|
||||
List<Author> 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);
|
||||
}
|
||||
Reference in New Issue
Block a user