@RestController
@RequiredArgsConstructor
public class OrderSimpleApiController {
private final OrderRepository orderRepository;
@GetMapping("/api/v4/simple-orders")
public List<OrderSimpleQueryDto> ordersV4() {
return orderRepository.findOrderDtos();
}
}
@Repository
@RequiredArgsConstructor
public class OrderSimpleQueryRepository {
public List<OrderSimpleQueryDto> findOrderDtos() {
return em.createQuery(
// 원하는 필드만 DTO에 정의해서 가져올 수 있다.
"select new jpabook.jpashop.repository.order.simplequery.OrderSimpleQueryDto(o.id, m.name, o.status, o.orderDate, d.address)"
+ " from Order o"
+ " join o.member m"
+ " join o.delivery d", OrderSimpleQueryDto.class)
.getResultList();
}
}
@Data
public class OrderSimpleQueryDto {
private Long orderId;
private String name;
private OrderStatus orderStatus;
private LocalDateTime orderDate;
private Address address;
public OrderSimpleQueryDto(Long orderId, String name, OrderStatus orderStatus, LocalDateTime orderDate, Address address) {
this.orderId = orderId;
this.name = name;
this.orderStatus = orderStatus;
this.orderDate = orderDate;
this.address = address;
}
}
@Repository
@RequiredArgsConstructor
public class OrderRepository {
public List<Order> findAllWithMemberDelivery() {
return em.createQuery(
"select o from Order o" +
" join fetch o.member m" +
" join fetch o.delivery d", Order.class)
.getResultList();
}
}
@Repository
@RequiredArgsConstructor
public class OrderSimpleQueryRepository {
public List<OrderSimpleQueryDto> findOrderDtos() {
return em.createQuery(
"select new jpabook.jpashop.repository.order.simplequery.OrderSimpleQueryDto(o.id, m.name, o.status, o.orderDate, d.address)"
+ " from Order o"
+ " join o.member m"
+ " join o.delivery d", OrderSimpleQueryDto.class)
.getResultList();
}
}