@GetMapping("/api/v2/orders")
public List<OrderDto> ordersV2(){
List<Order> orders = orderRepository.findAllString(new OrderSearch());
return orders.stream()
.map(o-> new OrderDto(o))
.collect(Collectors.toList());
}
변환 작업을 거쳐야 한다. OrderDto라는 것을 만들었다.
그런데, Order에 있는 OrderItems 또한 엔티티이다. 그래서 이것 또한 변환작업을 해야 한다.
@Getter
static class OrderDto{
private Long orderId;
private String name;
private LocalDateTime orderDate;
private OrderStatus orderStatus;
private Address address;
private List<OrderItemDto> orderItems;
OrderDto(Order order){
this.orderId = order.getId();
this.name = order.getMember().getName();
this.orderDate = order.getOrderDate();
this.orderStatus = order.getStatus();
this.address = order.getDelivery().getAddress();
order.getOrderItems().stream().forEach(o->o.getItem().getName());
this.orderItems = order.getOrderItems().stream()
.map(orderItem-> new OrderItemDto(orderItem))
.collect(Collectors.toList());
}
}
@Getter
static class OrderItemDto{
private String itemName;
private int orderPrice;
private int count;
public OrderItemDto(OrderItem orderItem){
this.itemName = orderItem.getItem().getName();
this.orderPrice = orderItem.getOrderPrice();
this.count = orderItem.getCount();
}
}
이렇게 만들었다.
OrderDto에서 OrderItems를 받을 때 마찬가지로 Dto로 만드는 작업을 해 준다.
그러면,
[
{
"orderId": 1,
"name": "userA",
"orderDate": "2023-11-14T13:14:20.329213",
"orderStatus": "ORDER",
"address": {
"city": "서울",
"street": "1",
"zipcode": "1111"
},
"orderItems": [
{
"itemName": "JPA1 BOOK",
"orderPrice": 10000,
"count": 1
},
{
"itemName": "JPA2 BOOK",
"orderPrice": 20000,
"count": 2
}
]
},
{
"orderId": 2,
"name": "userB",
"orderDate": "2023-11-14T13:14:20.411167",
"orderStatus": "ORDER",
"address": {
"city": "진주",
"street": "2",
"zipcode": "2222"
},
"orderItems": [
{
"itemName": "SPRING1 BOOK",
"orderPrice": 20000,
"count": 3
},
{
"itemName": "SPRING2 BOOK",
"orderPrice": 40000,
"count": 4
}
]
}
]
이렇게 깔끔하게 나온다.
하지만,
select
o1_0.order_id,
o1_0.delivery_id,
o1_0.member_id,
o1_0.order_date,
o1_0.status
from
orders o1_0
join
member m1_0
on m1_0.member_id=o1_0.member_id
fetch
first ? rows only
2023-11-14T13:15:01.550+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [INTEGER] - [1000]
2023-11-14T13:15:01.551+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301551 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 join member m1_0 on m1_0.member_id=o1_0.member_id fetch first ? rows only
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 join member m1_0 on m1_0.member_id=o1_0.member_id fetch first 1000 rows only;
2023-11-14T13:15:01.552+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
m1_0.member_id,
m1_0.city,
m1_0.street,
m1_0.zipcode,
m1_0.name
from
member m1_0
where
m1_0.member_id=?
2023-11-14T13:15:01.552+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [1]
2023-11-14T13:15:01.553+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301553 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select m1_0.member_id,m1_0.city,m1_0.street,m1_0.zipcode,m1_0.name from member m1_0 where m1_0.member_id=?
select m1_0.member_id,m1_0.city,m1_0.street,m1_0.zipcode,m1_0.name from member m1_0 where m1_0.member_id=1;
2023-11-14T13:15:01.553+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
d1_0.delivery_id,
d1_0.city,
d1_0.street,
d1_0.zipcode,
d1_0.status
from
delivery d1_0
where
d1_0.delivery_id=?
2023-11-14T13:15:01.554+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [1]
2023-11-14T13:15:01.554+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301554 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select d1_0.delivery_id,d1_0.city,d1_0.street,d1_0.zipcode,d1_0.status from delivery d1_0 where d1_0.delivery_id=?
select d1_0.delivery_id,d1_0.city,d1_0.street,d1_0.zipcode,d1_0.status from delivery d1_0 where d1_0.delivery_id=1;
2023-11-14T13:15:01.555+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
o1_0.order_id,
o1_0.delivery_id,
o1_0.member_id,
o1_0.order_date,
o1_0.status
from
orders o1_0
where
o1_0.delivery_id=?
2023-11-14T13:15:01.555+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [1]
2023-11-14T13:15:01.555+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301555 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 where o1_0.delivery_id=?
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 where o1_0.delivery_id=1;
2023-11-14T13:15:01.556+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
o1_0.order_id,
o1_0.order_item_id,
o1_0.count,
o1_0.item_id,
o1_0.order_price
from
order_item o1_0
where
o1_0.order_id=?
2023-11-14T13:15:01.557+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [1]
2023-11-14T13:15:01.557+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301557 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select o1_0.order_id,o1_0.order_item_id,o1_0.count,o1_0.item_id,o1_0.order_price from order_item o1_0 where o1_0.order_id=?
select o1_0.order_id,o1_0.order_item_id,o1_0.count,o1_0.item_id,o1_0.order_price from order_item o1_0 where o1_0.order_id=1;
2023-11-14T13:15:01.558+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
i1_0.item_id,
i1_0.dtype,
i1_0.name,
i1_0.price,
i1_0.stock_quantity,
i1_0.artist,
i1_0.etc,
i1_0.author,
i1_0.isbn,
i1_0.actor,
i1_0.director
from
item i1_0
where
i1_0.item_id=?
2023-11-14T13:15:01.558+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [1]
2023-11-14T13:15:01.558+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301558 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=?
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=1;
2023-11-14T13:15:01.559+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
i1_0.item_id,
i1_0.dtype,
i1_0.name,
i1_0.price,
i1_0.stock_quantity,
i1_0.artist,
i1_0.etc,
i1_0.author,
i1_0.isbn,
i1_0.actor,
i1_0.director
from
item i1_0
where
i1_0.item_id=?
2023-11-14T13:15:01.559+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [2]
2023-11-14T13:15:01.559+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301559 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=?
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=2;
2023-11-14T13:15:01.560+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
m1_0.member_id,
m1_0.city,
m1_0.street,
m1_0.zipcode,
m1_0.name
from
member m1_0
where
m1_0.member_id=?
2023-11-14T13:15:01.560+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [2]
2023-11-14T13:15:01.560+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301560 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select m1_0.member_id,m1_0.city,m1_0.street,m1_0.zipcode,m1_0.name from member m1_0 where m1_0.member_id=?
select m1_0.member_id,m1_0.city,m1_0.street,m1_0.zipcode,m1_0.name from member m1_0 where m1_0.member_id=2;
2023-11-14T13:15:01.561+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
d1_0.delivery_id,
d1_0.city,
d1_0.street,
d1_0.zipcode,
d1_0.status
from
delivery d1_0
where
d1_0.delivery_id=?
2023-11-14T13:15:01.561+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [2]
2023-11-14T13:15:01.561+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301561 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select d1_0.delivery_id,d1_0.city,d1_0.street,d1_0.zipcode,d1_0.status from delivery d1_0 where d1_0.delivery_id=?
select d1_0.delivery_id,d1_0.city,d1_0.street,d1_0.zipcode,d1_0.status from delivery d1_0 where d1_0.delivery_id=2;
2023-11-14T13:15:01.562+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
o1_0.order_id,
o1_0.delivery_id,
o1_0.member_id,
o1_0.order_date,
o1_0.status
from
orders o1_0
where
o1_0.delivery_id=?
2023-11-14T13:15:01.562+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [2]
2023-11-14T13:15:01.563+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301563 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 where o1_0.delivery_id=?
select o1_0.order_id,o1_0.delivery_id,o1_0.member_id,o1_0.order_date,o1_0.status from orders o1_0 where o1_0.delivery_id=2;
2023-11-14T13:15:01.564+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
o1_0.order_id,
o1_0.order_item_id,
o1_0.count,
o1_0.item_id,
o1_0.order_price
from
order_item o1_0
where
o1_0.order_id=?
2023-11-14T13:15:01.564+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [2]
2023-11-14T13:15:01.565+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301565 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select o1_0.order_id,o1_0.order_item_id,o1_0.count,o1_0.item_id,o1_0.order_price from order_item o1_0 where o1_0.order_id=?
select o1_0.order_id,o1_0.order_item_id,o1_0.count,o1_0.item_id,o1_0.order_price from order_item o1_0 where o1_0.order_id=2;
2023-11-14T13:15:01.566+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
i1_0.item_id,
i1_0.dtype,
i1_0.name,
i1_0.price,
i1_0.stock_quantity,
i1_0.artist,
i1_0.etc,
i1_0.author,
i1_0.isbn,
i1_0.actor,
i1_0.director
from
item i1_0
where
i1_0.item_id=?
2023-11-14T13:15:01.566+09:00 TRACE 7604 --- [nio-8080-exec-3] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [3]
2023-11-14T13:15:01.566+09:00 INFO 7604 --- [nio-8080-exec-3] p6spy : #1699935301566 | took 0ms | statement | connection 8| url jdbc:h2:tcp://localhost/./jpashop
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=?
select i1_0.item_id,i1_0.dtype,i1_0.name,i1_0.price,i1_0.stock_quantity,i1_0.artist,i1_0.etc,i1_0.author,i1_0.isbn,i1_0.actor,i1_0.director from item i1_0 where i1_0.item_id=3;
2023-11-14T13:15:01.567+09:00 DEBUG 7604 --- [nio-8080-exec-3] org.hibernate.SQL :
select
i1_0.item_id,
i1_0.dtype,
i1_0.name,
i1_0.price,
i1_0.stock_quantity,
i1_0.artist,
i1_0.etc,
i1_0.author,
i1_0.isbn,
i1_0.actor,
i1_0.director
from
item i1_0
where
i1_0.item_id=?
쿼리가 상당히 많이 나간다.
엔티티를 노출하지 말라는 게, 말 그대로 엔티티들은 노출하지 말라는 거다. 그래서 엔티티인 OrderItem도 저렇게 바꿔 줄 필요가 있다.
'스프링데이터 + JPA > API 개발' 카테고리의 다른 글
13. 컬렉션 페이징 한계돌파 (0) | 2023.11.15 |
---|---|
12. 컬렉션 -> 엔티티 join fetch 최적화 (0) | 2023.11.15 |
10. 컬렉션 조회 최적화 (0) | 2023.11.14 |
9. 바로 Dto로 받기 (0) | 2023.11.14 |
8. fetch join 최적화 (0) | 2023.11.13 |