티스토리 뷰
QueryDSL
기본
- user define repository interface
public interface MemberRepositoryCustom {
Member search(String memberName);
}
- user define repository interface implement
public class MemberRepositoryImpl implements MemberRepositoryCustom {
@PersistenceContext(unitName = "jpaPractice")
private EntityManager entityManager;
public Member findOne(String memberName) {
JPAQuery jpaQuery = new JPAQuery(entityManager);
QMember member = QMember.member;
BooleanBuilder condition = new BooleanBuilder();
condition.and(equal(member.name, memberName));
jpaQuery.from(member)
.where(condition);
return jpaQuery.singleResult(member);
}
}
- using user define repository interface
MemberRepository extend JpaRepository<Member, Long>, MemberRepositoryCustom
memberRepository.search("memberName");
응용
public interface OrderRepositoryCustom {
List<Order> search(OrderSearch orderSearch);
}
public class OrderRepositoryImpl implements OrderRepositoryCustom {
@PersistenceContext(unitName = "jpaPractice")
private EntityManager entityManager;
@Override
public List<Order> search(OrderSearch orderSearch) {
JPAQuery jpaQuery = new JPAQuery(entityManager);
QOrder qOrder = QOrder.order;
QMember qMember = QMember.member;
jpaQuery.from(qOrder)
.innerJoin(qOrder.member, qMember)
.where(OrderPredicateConverter.convert(orderSearch));
return jpaQuery.list(qOrder);
}
}
public class OrderPredicateConverter {
public static Predicate convert(OrderSearch orderSearch) {
QOrder qOrder = QOrder.order;
QMember qMember = QMember.member;
BooleanBuilder condition = new BooleanBuilder();
condition.and(PredicateBuilder.equal(qOrder.status, orderSearch.getOrderStatus()));
condition.and(PredicateBuilder.equal(qMember.name, orderSearch.getMemberName()));
return condition;
}
}
'Programming > JPA' 카테고리의 다른 글
Embedded Mapping (0) | 2019.05.29 |
---|---|
Discriminate Mapping (0) | 2019.05.26 |
Query DSL, Criteria 설정 (0) | 2019.03.01 |
Hibernate Criteria (0) | 2018.08.05 |
Hibernate @Version (0) | 2018.07.16 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- SmartLifecycle
- scikit-learn
- DI
- Spring JDBC Template
- Embeddable Mapping
- JPA
- Registrar
- java generic
- Sprint RetryTemplate
- java EqualsAndHashCode
- Embedded Mapping
- @Primary
- Join Table
- Mapping
- docker
- spring spel
- Discriminate Mapping
- RetryTemplate
- Akka
- java Equals
- Query DSL
- Charles proxy
- JPA Criteria
- Spring Registrar
- Property
- guava
- Typesafe Config
- Criteria
- 복합키 Mapping
- Spring
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함