repository > order > query > OrderQueryRepository
: 엔티티가 아닌 화면 관련됐을 때
repository > order > OrderRepository
: 핵심 비즈니스 로직 엔티티 관련
@GetMapping("/api/v4/orders")
public List<OrderQueryDto> ordersV4(){
return orderQueryRepository.findOrderQueryDtos();
}
OrderQueryRepository
/**
* 1:N 관계(컬렉션)를 제외한 나머지를 한번에 조회
*/
private List<OrderItemQueryDto> findOrderItems(Long orderId) {
return em.createQuery(
"select new jpabook.jpashop.repository.order.query.OrderItemQueryDto(oi.order.id, i.name, oi.orderPrice, oi.count) " +
"from OrderItem oi" +
"join oi.item i " +
"where oi.order.id =:orderId", OrderItemQueryDto.class)
.setParameter("orderId",orderId)
.getResultList();
}
/**
* 1:N 관계인 orderItems 조회
*/
private List<OrderQueryDto> findOrders() {
return em.createQuery(
"select new jpabook.jpashop.repository.order.query.OrderQueryDto(o.id, m.name, o.orderDate, o.status, d.address)" +
"from Order o" +
"join o.member m" +
"join o.delivery d", OrderQueryDto.class)
.getResultList();
}
ToOne(N:1, 1:1) 관계들을 먼저 조회하고, ToMany(1:N) 관계는 각각 별도로 처리함
컬렉션은 별도로 조회
Query: 루트 1번, 컬렉션 N 번
단건 조회에서 많이 사용하는 방식
-- 루트 조회(toOne 코드를 모두 한번에 조회)
select
order0_.order_id as col_0_0_,
member1_.name as col_1_0_,
order0_.order_date as col_2_0_,
order0_.status as col_3_0_,
delivery2_.city as col_4_0_,
delivery2_.street as col_4_1_,
delivery2_.zipcode as col_4_2_
from
orders order0_
inner join
member member1_
on order0_.member_id=member1_.member_id
inner join
delivery delivery2_
on order0_.delivery_id=delivery2_.delivery_id
--루프를 돌면서 컬렉션 추가(추가 쿼리 실행)
select
orderitem0_.order_id as col_0_0_,
item1_.name as col_1_0_,
orderitem0_.order_price as col_2_0_,
orderitem0_.count as col_3_0_
from
order_item orderitem0_
inner join
item item1_
on orderitem0_.item_id=item1_.item_id
where
orderitem0_.order_id=?
-- 루프를 돌면서 컬렉션 추가(추가 쿼리 실행)
select
orderitem0_.order_id as col_0_0_,
item1_.name as col_1_0_,
orderitem0_.order_price as col_2_0_,
orderitem0_.count as col_3_0_
from
order_item orderitem0_
inner join
item item1_
on orderitem0_.item_id=item1_.item_id
where
orderitem0_.order_id=?