[스프링] 스프링 데이터 - 스프링 데이터 JPA와 QueryDSL
June 7, 2022
애그리거트(Aggregate)와 명세(Specification)
도메인 주도 설계에서의 애그리거트와 명세
불리언 로직을 사용하여 비즈니스 규칙들을 체이닝 형태로 결합할 수 있게 만드는 것을 명세 패턴(specification pattern)이라고 함Wiki
스프링은 Specification 클래스에 해당 개념을 정의
Specification 객체는 and(), not(), or(), where() 메서드를 통해 서로 조합 가능함
Specification 객체 조합 후 toPredicate() 메서드를 호출하여 조합한 Specification 객체를 Predicate 객체로 변환
스프링 데이터 JPA에서 JPA Criteria API 기반 명세 사용 방법
Specification 인터페이스 구현 객체 정의
javax.persistence.criteria.Predicate 객체(명세, 검색 조건)를 반환하는 Specification의 toPredicate() 메서드 구현
toPredicate() 메서드에 명세(검색 조건)을 정의
JpaRepository 인터페이스를 상속하는 레포지토리 인터페이스에서 JpaSpecificationExecutor 인터페이스 상속
JpaSpecificationExecutor의 메소드 호출 시 인자로 Specification 객체 전달하여 검색 조건으로 사용
스프링 데이터 JPA에서 QueryDSL 기반 명세 사용 방법
Specification 인터페이스 구현 객체 정의
com.querydsl.core.types.Predicate 객체(명세, 검색 조건)를 반환하는 Specification의 toPredicate() 메서드 구현
toPredicate() 메서드에 명세(검색 조건)을 정의
JpaRepository 인터페이스를 상속하는 레포지토리 인터페이스에서 QuerydslPredicateExecutor 인터페이스 상속
QuerydslPredicateExecutor 메소드 호출 시 인자로 Specification 객체 전달하여 검색 조건으로 사용
스프링 데이터 JPA에서 QueryDSL 사용 방법
QuerydslPredicateExecutorDoc
QueryDslRepositorySupport
QueryFactory
QueryDSL의 BooleanBuilder, BooleanExpression
참고
Comments