diff --git a/src/main/java/com/ankurm/sdjpa4demo/repo/BookRepository.java b/src/main/java/com/ankurm/sdjpa4demo/repo/BookRepository.java index 2cf2f87..232f02f 100644 --- a/src/main/java/com/ankurm/sdjpa4demo/repo/BookRepository.java +++ b/src/main/java/com/ankurm/sdjpa4demo/repo/BookRepository.java @@ -1,16 +1,29 @@ package com.ankurm.sdjpa4demo.repo; import com.ankurm.sdjpa4demo.domain.Book; +import com.ankurm.sdjpa4demo.domain.BookSummary; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.math.BigDecimal; import java.util.List; public interface BookRepository extends JpaRepository { - List findByPriceGreaterThanEqual(BigDecimal price); + // Embedded-value-object path traversal: price -> amount. Resolves to: where b.price.amount >= :min + List findByPriceAmountGreaterThanEqual(BigDecimal min); - // Single-result derived query that can match MORE than one row on bad data: + // Single-result derived query over an embedded path that can match MORE than one row on bad data: // demonstrates IncorrectResultSizeDataAccessException (the "non-unique" edge case). - Book findByPrice(BigDecimal price); + Book findByPriceAmount(BigDecimal price); + + // CORNER CASE: a derived-query class-based (record) projection matches constructor-parameter names + // against DIRECT entity properties only. BookSummary's "amount" component does not resolve against + // the nested b.price.amount path this way - PropertyReferenceException: No property 'amount' found + // for type 'Book' (verified by actually running the derived-method form before falling back to this). + // A @Query constructor expression works for nested/embedded paths; the derived-method-only form does not. + @Query("select new com.ankurm.sdjpa4demo.domain.BookSummary(b.title, b.price.amount) " + + "from Book b where b.price.amount <= :max") + List findByPriceAmountLessThanEqual(@Param("max") BigDecimal max); }