인프런 커뮤니티 질문&답변

한창훈님의 프로필 이미지
한창훈

작성한 질문수

실전! 스프링 부트와 JPA 활용2 - API 개발과 성능 최적화

간단한 주문 조회 V1: 엔티티를 직접 노출

21:59에 lazy 초기화 오류

해결된 질문

작성

·

398

0


[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? 예
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? 예
3. 질문 잘하기 메뉴얼을 읽어보셨나요? 예

[질문 내용]

21:59초에 영상 내용을 따라 Lazy 초기화 후에 포스트맨을 검색해보았는데 출력이 되지 않고 오류가 떠서 질문드립니다.. 오류를 보아하니 무한루프가 돌아가는것 같은 느낌도 듭니다..

오류내용

java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

 

2023-10-24 12:44:32.150 ERROR 46125 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->jpabook.jpashop.domain.Delivery["order"]->jpabook.jpashop.domain.Order["delivery"]->

 

initDb

package jpabook.jpashop;
import jpabook.jpashop.domain.*;
import jpabook.jpashop.domain.Item.Book;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
@Component
@RequiredArgsConstructor
public class InitDb {
    private final InitService initService;
    @PostConstruct
    public void init() {
        initService.dbInit1();
        initService.dbInit2();
    }
    @Component
    @Transactional
    @RequiredArgsConstructor
    static class InitService {
        private final EntityManager em;
        public void dbInit1() {
            Member member = createMember("userA", "서울", "1", "1111");
            em.persist(member);
            Book book1 = createBook("JPA1 BOOK", 10000, 100);
            em.persist(book1);
            Book book2 = createBook("JPA2 BOOK", 20000, 100);
            em.persist(book2);

            OrderItem orderItem1 = OrderItem.createOrderItem(book1, 10000, 1);
            OrderItem orderItem2 = OrderItem.createOrderItem(book2, 20000, 2);

            Order order = Order.createOrder(member, createDelivery(member),
                    orderItem1, orderItem2);
            em.persist(order);
        }
        public void dbInit2() {
            Member member = createMember("userB", "진주", "2", "2222");
            em.persist(member);
            Book book1 = createBook("SPRING1 BOOK", 20000, 200);
            em.persist(book1);
            Book book2 = createBook("SPRING2 BOOK", 40000, 300);
            em.persist(book2);
            Delivery delivery = createDelivery(member);
            OrderItem orderItem1 = OrderItem.createOrderItem(book1, 20000, 3);
            OrderItem orderItem2 = OrderItem.createOrderItem(book2, 40000, 4);
            Order order = Order.createOrder(member, delivery, orderItem1,
                    orderItem2);
            em.persist(order);
        }
        private Member createMember(String name, String city, String street,
                                    String zipcode) {
            Member member = new Member();
            member.setName(name);
            member.setAddress(new Address(city, street, zipcode));
            return member;
        }
        private Book createBook(String name, int price, int stockQuantity) {
            Book book = new Book();
            book.setName(name);
            book.setPrice(price);
            book.setStockQuantity(stockQuantity);
            return book;
        }
        private Delivery createDelivery(Member member) {
            Delivery delivery = new Delivery();
            delivery.setAddress(member.getAddress());
            return delivery;
        }
    }
}

 

OrderSimpleApiController

package jpabook.jpashop.api;

import jpabook.jpashop.domain.Order;
import jpabook.jpashop.repository.OrderRepository;
import jpabook.jpashop.repository.OrderSearch;
import lombok.RequiredArgsConstructor;
import org.aspectj.weaver.ast.Or;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * XToOne
 * Order
 * Order -> Member
 * Order -> Delivery
 */
@RestController
@RequiredArgsConstructor
public class OrderSimpleApiController {

    private final OrderRepository orderRepository;

    @GetMapping("/api/v1/simple-orders")
    public List<Order> ordersV1(){
        List<Order> all = orderRepository.findAllByString(new OrderSearch());
        for (Order order : all) {
            order.getMember().getName();  //Lazy 강제 초기화
            order.getDelivery().getAddress();  //Lazy 강제 초기화
        }
        return all;
    }
}

 

JpashopApplication

package jpabook.jpashop;

import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class JpashopApplication {

	public static void main(String[] args) {
		SpringApplication.run(JpashopApplication.class, args);
	}

	@Bean
	Hibernate5Module hibernate5Module(){

		Hibernate5Module hibernate5Module = new Hibernate5Module();
		return hibernate5Module;

	}
}

코드도 같이 올렸습니다.. 무엇이 다른건지 잘 모르겠습니다..

답변 1

0

안녕하세요. 한창훈님, 공식 서포터즈 y2gcoder입니다.

영한님께서 7분 50초 경 부터 @JsonIgnore를 달아주고 있습니다.

누락된 부분은 없는지 확인해보시겠습니까?

  • Member 의 orders 위

  • OrderItem 의 order 위

  • Delivery의 order 위(여기가 유력할 것 같습니다!)

     

 

감사합니다.

한창훈님의 프로필 이미지
한창훈
질문자

감사합니다!! JsonIgnore 가 필요한 것이었군요

한창훈님의 프로필 이미지
한창훈

작성한 질문수

질문하기