1. Specification
- 도메인 객체에 대한 조건을 정의
public interface Specification<T> {
    Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder);
}
Predicate
- 각 조건을 표현하는 객체 (where 절)
CriteriaBuilder
- 다양한 조건을 빌드할 수 있는 객체
Definition
public class CustomerSpecs {
    // 특정 날짜 이전에 생성된 고객을 찾는 Specification
    public static Specification<Customer> isLongTermCustomer() {
        return (root, query, builder) -> {
            LocalDate date = LocalDate.now().minusYears(2);
            return builder.lessThan(root.get(Customer_.createdAt), date);
        };
    }
    // 특정 금액 이상의 매출을 가진 고객을 찾는 Specification
    public static Specification<Customer> hasSalesOfMoreThan(MonetaryAmount value) {
        return (root, query, builder) -> {
            // 쿼리 작성
        };
    }
}
2. JpaSpecificationExecutor
- Specification을 사용하여 쿼리를 실행할 수 있는 메서드 제공
- 동적 쿼리를 쉽게 작성 가능
public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
    // 커스텀 쿼리 메서드 정의
}
Use
List<Customer> customers = customerRepository.findAll(CustomerSpecs.isLongTermCustomer());MonetaryAmount amount = new MonetaryAmount(200.0, Currencies.DOLLAR);
List<Customer> customers = customerRepository.findAll(
    CustomerSpecs.isLongTermCustomer().or(CustomerSpecs.hasSalesOfMoreThan(amount))
);
출처
'Spring > Spring Data JPA' 카테고리의 다른 글
| [자바 ORM 표준 JPA 프로그래밍] 10-2. 객체지향 쿼리 언어: Criteria (0) | 2025.04.24 | 
|---|---|
| [자바 ORM 표준 JPA 프로그래밍] 10-1. 객체지향 쿼리 언어: JPQL (0) | 2025.04.24 | 
| [Spring Data JPA] 4-1. Spring Data Extensions (1) (1) | 2024.08.02 | 
| [Spring Data JPA] 3-3. Custom Repository Implementations (0) | 2024.08.02 | 
| [Spring Data JPA] 3-2. JPA Query Methods (4) (0) | 2024.08.02 |