I\'m working on an EclipseLink project in which one user can \"follow\" another as can be done on social media sites. I have this set up with a User
entity (referen
Every time you have @OneToMany
(a collection) you need to add @JsonIgnore
to it or else it will cause an infinite loop which results in a stack overflow exception because it keeps looking up between the parent(the one side) and the child (the many side)
For more info on dealing with this kind of problems check this excellent article http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
I tried everything what I found in the web and it didn't work. None of any annotation. But I found a solution after a huge fight with this issue.
First point you need to add to both entities (not the relational one) this annotation:
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler", "relationClass"})
public class YourClass { ...
}
Where "relationClass" is the name of the List/Set of your class for the relations:
For example:
@OneToMany(mappedBy = "yourClass", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Set<RelationClass> relationClass;
You also need to specify "hibernateLazyInitializer", "handler" in the annotation or it will cause serialization problem.
After it if you see the table that define relation table and you will see rows in there, and in your JSON response will not be any loop anymore. So as I think the best solution is also create a repository for the relation tablee and access the data from there.
Hope it will help someone!