BookRepository: embedded-path queries + record projection (with the nested-path projection corner case)
This commit is contained in:
@@ -1,16 +1,29 @@
|
|||||||
package com.ankurm.sdjpa4demo.repo;
|
package com.ankurm.sdjpa4demo.repo;
|
||||||
|
|
||||||
import com.ankurm.sdjpa4demo.domain.Book;
|
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.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface BookRepository extends JpaRepository<Book, Long> {
|
public interface BookRepository extends JpaRepository<Book, Long> {
|
||||||
|
|
||||||
List<Book> findByPriceGreaterThanEqual(BigDecimal price);
|
// Embedded-value-object path traversal: price -> amount. Resolves to: where b.price.amount >= :min
|
||||||
|
List<Book> 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).
|
// 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<BookSummary> findByPriceAmountLessThanEqual(@Param("max") BigDecimal max);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user