반응형
1. Projections.bean(dto.class, field1, field2)
DTO setter 이용
List<UserDTO> dtos = query.select(
Projections.bean(UserDTO.class, user.firstName, user.lastName)).fetch();
2. Projections.fields(dto.class, field1, field2)
DTO setter 대신 필드를 직접 사용하는 경우, setter 없어도 됨
List<UserDTO> dtos = query.select(
Projections.fields(UserDTO.class, user.firstName, user.lastName)).fetch();
3. Projections.constructor(dto.class, field1, field2)
생성자 이용, 생성자 파라미터와 순서가 같아야함
List<UserDTO> dtos = query.select(
Projections.constructor(UserDTO.class, user.firstName, user.lastName)).fetch();
4. @QueryProjection 어노테이션 with DTO 생성자
DTO 생성자에 @QueryProjection 어노테이션 달기
class CustomerDTO {
@QueryProjection
public CustomerDTO(long id, String name) {
...
}
}
조회시 new 이용하여 생성자 사용
QCustomer customer = QCustomer.customer;
JPQLQuery query = new HibernateQuery(session);
List<CustomerDTO> dtos = query.select(new QCustomerDTO(customer.id, customer.name))
.from(customer).fetch();
5. @QueryProjection 어노테이션 with Entity 생성자
Entity 생성자에 @QueryProjection 어노테이션 달기
@Entity
class Customer {
@QueryProjection
public Customer(long id, String name) {
...
}
}
QEntity.create(entity.field1, entity.field2)
QCustomer customer = QCustomer.customer;
JPQLQuery query = new HibernateQuery(session);
List<Customer> dtos = query.select(QCustomer.create(customer.id, customer.name))
.from(customer).fetch();
[참고 - Querydsl 레퍼런스 가이드]
http://querydsl.com/static/querydsl/5.0.0/reference/html_single/
Querydsl Reference Guide
The Java 6 APT annotation processing functionality is used in Querydsl for code generation in the JPA, JDO and Mongodb modules. This section describes various configuration options for the code generation and an alternative to APT usage. 3.3.1. Path initi
querydsl.com
반응형
'개발 > JPA' 카테고리의 다른 글
[JPA] 상속관계 매핑 - 상속 전략 (0) | 2022.04.06 |
---|---|
[JPA] 영속성 컨텍스트 / 엔티티의 생명주기 (0) | 2022.03.31 |
[JPA] Spring Data JPA - Query Method / 쿼리 메소드 / 간단한 쿼리는 쿼리 메소드 사용하면 편리 (0) | 2022.01.20 |
[JPA] enum 타입 DB에 저장시 값 변환 / @Converter / AttributeConverter (0) | 2022.01.19 |
[JPA] postgres 스키마 설정 / default schema 설정 (0) | 2022.01.17 |
댓글