티스토리 뷰
Criteria
기본
- Specification
public Specification<Member> memberNameLike(String mamberName) {
return (root, query, cb) -> cb.equal(root.get(Member_.memberName), memberName);
}
- Finder
memberRepository.findAll(memberNameLike(memberName), pageable);
응용
public class QuerySupporter<T> {
public Specifications<T> combineSpecifications(Specification<T>... specifications) {
return combineSpecifications(Lists.newArrayList(specifications));
}
public Specifications<T> combineSpecifications(List<Specification<T>> specifications) {
List<Specification<T>> specificationList = specifications.stream()
.filter(trigger -> !isEmpty(trigger))
.collect(Collectors.toList());
return specificationList.isEmpty() ? null : combineSpecifications(specificationList.iterator());
}
public Specifications<T> combineSpecifications(Iterator<Specification<T>> iterator) {
Specifications<T> where = Specifications.where(iterator.next());
while (iterator.hasNext()) {
where = where.and(iterator.next());
}
return where;
}
public Specification<T> equalLongSpecifications(SingularAttribute<T, Long> attribute, Long value) {
return ObjectUtils.isEmpty(value) ? null : (root, query, cb) -> cb.equal(root.get(attribute), value);
}
public Specification<T> equalStringSpecifications(SingularAttribute<T, String> attribute, String value) {
return ObjectUtils.isEmpty(value) ? null : (root, query, cb) -> cb.equal(root.get(attribute), value);
}
public Specification<T> equalBooleanSpecifications(SingularAttribute<T, Boolean> attribute, Boolean value) {
return ObjectUtils.isEmpty(value) ? null : (root, query, cb) -> cb.equal(root.get(attribute), value);
}
public Specification<T> likeSpecifications(SingularAttribute<T, String> attribute, String value) {
return Strings.isNullOrEmpty(value) ? null : (root, query, cb) -> cb.like(root.get(attribute), "%" + value + "%");
}
}
public class MemberQuerySupporter extends QuerySupporter<Member> {
public Specification<Member> searchSpecifications(Long memberId, String memberName) {
return combineSpecifications(memberIdEqual(memberId), memberNameLike(memberName));
}
public Specification<Member> memberIdEqual(Long memberId) {
return equalLongSpecifications(Member_.memberId, memberId);
}
public Specification<Member> memberNameLike(String memberName) {
return likeSpecifications(Member_.memberName, memberName);
}
}
'Programming > JPA' 카테고리의 다른 글
Embedded Mapping (0) | 2019.05.29 |
---|---|
Discriminate Mapping (0) | 2019.05.26 |
Query DSL, Criteria 설정 (0) | 2019.03.01 |
Query DSL (0) | 2018.08.05 |
Hibernate @Version (0) | 2018.07.16 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- DI
- RetryTemplate
- @Primary
- java Equals
- Query DSL
- Spring
- JPA Criteria
- JPA
- Sprint RetryTemplate
- Criteria
- guava
- Registrar
- Embedded Mapping
- java EqualsAndHashCode
- spring spel
- Embeddable Mapping
- Spring Registrar
- Typesafe Config
- SmartLifecycle
- docker
- Property
- Akka
- Join Table
- Mapping
- Charles proxy
- Spring JDBC Template
- java generic
- Discriminate Mapping
- 복합키 Mapping
- scikit-learn
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함