Open devYuraKim opened 1 month ago
Then ChatGTP offered some great solution which worked like magic!
The @JsonManagedReference
and @JsonBackReference
annotations in Jackson are used to handle bidirectional relationships in JSON serialization, and they help to prevent infinite recursion and infinite loops that can occur with such relationships. Here's a detailed explanation of why these annotations fixed the problem:
The Problem with Bidirectional Relationships
When you have a bidirectional relationship between entities (e.g., EazyClass
and Person
), serializing these entities to JSON can create a loop:
EazyClass
has a reference to Person
instances.Person
instance has a reference back to the EazyClass
.If you try to serialize this bidirectional relationship, Jackson will keep following the references back and forth between EazyClass
and Person
, leading to infinite recursion. This can result in errors like StackOverflowError
, or excessively large JSON outputs that can cause performance issues or crashes.
Person.java
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "class_id", referencedColumnName = "classId", nullable = true)
@JsonBackReference
private EazyClass eazyClass;
EazyClass.java
@OneToMany(mappedBy = "eazyClass", cascade = CascadeType.PERSIST,targetEntity = Person.class)
@JsonManagedReference
private Set<Person> persons;
But I definitely need to learn more about this issue.
[[JPA]쏘아올린 JPA의 순환 참조, @JsonManagedReference에서 볼까, DTO로 볼까] https://velog.io/@power0080/JPA쏘아올린-JPA의-순환-참조-JsonManagedReference에서-볼까-DTO로-볼까
[JPA] 순환참조와 해결 방법 @JsonIgnore, @JsonManagedReference, @JsonBackReference https://m.blog.naver.com/writer0713/221587351970
Jackson – Bidirectional Relationships https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
I wanted to check for myself what kind of information gets stored in HttpSession, so I added in
SessionController.java
.The results were nice and pretty
up until
session.setAttribute("eazyClass", eazyClass);
fromAdminController.java
was executed. AdminController.javaIt seemed like the output kept repeating itself until it couldn't anymore and then just crashed.