๐Ÿ“Œ [MarketBridge] 02. ๊ฐœ๋ฐœ์ผ์ง€ - ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์˜ ๋ชฐ๋ž๋˜ ์‚ฌ์‹ค..!

๋ชจ๊น…ยท2024๋…„ 1์›” 24์ผ
0

๊ฐœ๋ฐœ์ผ์ง€ - MarketBridge

๋ชฉ๋ก ๋ณด๊ธฐ
2/8
post-thumbnail
post-custom-banner

๐Ÿ“– ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์˜ ๋ชฐ๋ž๋˜ ์‚ฌ์‹ค..!

	@Test
    @DisplayName("์œ ์ €๊ฐ€ ๋ฐ˜ํ’ˆ, ์ทจ์†Œํ•œ ์ƒํ’ˆ๋“ค์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. ")
    public void findDistinctWithDetailsByMemberId() {
        // given
        Member member = Member.builder().build();

        Order order = Order.builder()
                .member(member)
                .orderNo("123")
                .build();

        Product product1 = Product.builder().build();
        Product product2 = Product.builder().build();
        Product product3 = Product.builder().build();

        OrderDetail orderDetail1 = OrderDetail.builder()
                .order(order)
                .product(product1)
                .statusCode(StatusCodeType.RETURN_COMPLETED.getCode())
                .build();
        OrderDetail orderDetail2 = OrderDetail.builder()
                .order(order)
                .product(product2)
                .statusCode(StatusCodeType.ORDER_CANCEL.getCode())
                .build();
        OrderDetail orderDetail3 = OrderDetail.builder()
                .order(order)
                .product(product3)
                .statusCode(StatusCodeType.DELIVERY_DELAY.getCode())
                .build();

        order.addOrderDetail(orderDetail1);
        order.addOrderDetail(orderDetail2);
        order.addOrderDetail(orderDetail3);

        productRepository.saveAll(List.of(product1, product2, product3));
        memberRepository.save(member);
        orderRepository.save(order);

        // when
        List<Order> orders = orderRepository.findDistinctWithDetailsByMemberId(member.getId());

        // then
        assertThat(orders).hasSize(1)
                .extracting("orderNo")
                .contains("123");

        List<OrderDetail> orderDetails = orders.get(0).getOrderDetails();
        assertThat(orderDetails).hasSize(2);

    }
	@Override
    public List<Order> findDistinctWithDetailsByMemberId(Long memberId) {

        BooleanExpression statusCondition = orderDetail.statusCode.eq(ORDER_CANCEL.getCode());
        BooleanExpression orCondition = statusCondition.or(orderDetail.statusCode.eq(RETURN_COMPLETED.getCode()));

        return queryFactory
                .selectDistinct(order)
                .from(order)
                .join(order.orderDetails, orderDetail).fetchJoin()
                .where(
                        order.member.id.eq(memberId),
                        orCondition
                )
                .fetch();
    }

memberId์™€ ORDER_CANCEL,RETURN_COMPLETED ์ƒํƒœ๋กœ ๊ฑธ๋Ÿฌ์„œ ์ตœ์ข…์ ์œผ๋กœ ์›ํ•˜๋Š” orderDetail์ด 2๊ฐœ ๋‚˜์˜ฌ ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์˜ˆ์ƒ๊ณผ ๋‹ฌ๋ฆฌ 3๊ฐœ์˜ orderDetail์ด ๋‚˜์™”๋‹ค.

em.flush();
em.clear();

// when
List<Order> orders = orderRepository.findDistinctWithDetailsByMemberId(member.getId());

when์ ˆ ์ด์ „์— ๊ฐ•์ œ๋กœ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๋ฅผ ํ”Œ๋Ÿฌ์‹œํ•˜๊ณ  ํด๋ฆฌ์–ด ํ•ด์ฃผ๋‹ˆ ์˜ˆ์ƒ๋Œ€๋กœ orderDetail์ด 2๊ฐœ๊ฐ€ ๋ฐ˜ํ™˜๋˜์—ˆ๋‹ค.

JPA๊ฐ€ ๋‚ ๋ฆฐ ์ฟผ๋ฆฌ๋ฅผ ๊ทธ๋Œ€๋กœ db์— ๋ฐ˜์˜ํ•˜๋ฉด ์˜ˆ์ƒํ–ˆ๋˜๋Œ€๋กœ 2๊ฐœ์˜ orderDetail์ด ๋‚˜์˜จ๋‹ค. ์ฆ‰, ์ž‘์„ฑํ•œ ์ฟผ๋ฆฌ์—๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ–ˆ๋‹ค.

๋„๋Œ€์ฒด ์–ด๋””์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ผ๊นŒ? ์˜ˆ์ธกํ•˜๊ธฐ๋กœ๋Š” ์˜์†์„ฑ ์ปจํ…Œ์ŠคํŠธ์— ๋น„๋ฐ€์ด ๋ถ„๋ช…ํžˆ ์žˆ์„๊ฒƒ์œผ๋กœ ํŒ๋‹จํ–ˆ๋‹ค.

๋ฐ”๋กœ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์˜ ๋งค์ปค๋‹ˆ์ฆ˜ ๋•Œ๋ฌธ์ด์—ˆ๋‹ค.
์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๋ฅผ ํ”Œ๋Ÿฌ์‹œ, ํด๋ฆฌ์–ดํ•˜๊ธฐ ์ด์ „์— ๋ถ„๋ช…ํžˆ order๋Š” orderDetail์„ 3๊ฐœ ๊ฐ€์ง€๊ณ  ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์บ์‹ฑ๋˜์–ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ดํ›„ JPQL์„ ์ด์šฉํ•ด ํ•„ํ„ฐ๋งํ•ด์„œ ๊ฐ€์ ธ์˜ค๋”๋ผ๋„ ์ด๋ฏธ JPA๋Š” ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ๊ฐ™์€ @Id๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ db์—์„œ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ๋ฒ„๋ฆฐ ํ›„ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค..!

์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ข€ ๋” ๊ณต๋ถ€ํ•ด์„œ ์˜ฌ๋ ค๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค!!

profile
๋ฉˆ์ถ”์ง€ ์•Š๊ธฐ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€