순환 참조 및 프록시 직렬화 에러 확인 - 한재성 #10
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Jackson 순환 참조 문제 이해 및 해결
문제 및 에러 발생 확인
1. 순환 참조 문제
두 Entity Classroom과 Student는 양방향 연관관계로 구성
@OneToMany를 통해 여러 개의 Student Entity를 포함 -> 일대다 관계 형성@ManyToOne을 통해 하나의 Classroom Entity를 참조 -> 다대일 관계 형성순환 참조 발생
2. 프록시 직렬화 에러
프록시 직렬화 에러를 발생시키기 위해 코드 수정을 진행
@ManyToOne(fetch = FetchType.LAZY)설정(fetch의 기본값은FetchType.EAGER)@JsonIgnore설정프록시 직렬화 에러 발생
/api/students/{id}호출 시, Student 엔티티의 classRoom 필드는@ManyToOne(fetch = LAZY)로 설정되어 있어 프록시 객체(Hibernate의 ByteBuddy 기반 동적 프록시)로 초기화.이는 실제 ClassRoom 객체가 아니라, 내부적으로
ByteBuddyInterceptor를 통해 실제 데이터를 지연 로딩할 수 있는 구조로 되어 있음-> JSON 직렬화 시점에 프록시 객체는 일반 POJO(Plain Old Java Object)가 아니므로, Jackson은 내부 필드를 직렬화할 수 없어 예외를 던져 에러 발생.