[오류] JsonMappingException 오류 : infinite recursionWeb Program/Java2018. 8. 24. 22:49
Table of Contents
반응형
Data 는 File을 여러개 가지는 1:N 구조입니다.
아래와 같이 Data와 File은 양방향 설정을 한상태입니다.
public class Data {
…
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "data")
@JoinColumn(name="idx")
private List file;
}
public class File {
…
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@PrimaryKeyJoinColumn
private Data data;
}
public class DataService {
…
public List getData() {
List item = select()
.from(qData)
.join(qData.file, qFile).fetchJoin()
.fetch();
}
}
JPA의 @OneToMany 양방향 Data <-> File 설정 후 Join 시, 발생되는 JsonMappingException 오류
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:706)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112)
Caused by: java.lang.StackOverflowError: null
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:125)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at java.net.URLStreamHandler.parseURL(URLStreamHandler.java:254)
at sun.net.www.protocol.file.Handler.parseURL(Handler.java:67)
…
[http-nio-8080-exec-8] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper:logExceptions:127 SQL Error: 1406, SQLState: 22001
[http-nio-8080-exec-8] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper:logExceptions:129 Data truncation: Data too long for column 'TRACE' at row 1
애노테이션 @JsonManagedReference, @JsonBackReference 를 각각 붙여 줍니다.
public class Data {
…
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "data")
@JoinColumn(name="idx")
@JsonManagedReference
private List file;
}
public class File {
…
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@PrimaryKeyJoinColumn
@JsonBackReference
private Data data;
}
오류가 사라졌습니다, 썩세스!!
@달끄 :: 코트슈
코트슈(Code Troubleshooter) 프로그래머의 일상 Execution, Errors, Find, Solution 개발자들은 많은 오류로 시간을 허비한다... 코트슈가 도와드립니다.
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!