ARTICLE AD BOX
The Hibernate mapping itself looks fine. The recursion you're seeing is almost certainly happening during JSON serialization (Jackson), not in Hibernate itself. When Jackson serializes a DemoEntity, it serializes friends, which each have friendOf pointing back, which have friends, etc. → infinite loop.
You can break the cycle with @JsonIgnore on one side.
@ManyToMany @JoinTable(name = "friends", joinColumns = @JoinColumn(name = "id1"), inverseJoinColumns = @JoinColumn(name = "id2")) private List<DemoEntity> friends; @JsonIgnore // Don't serialize this side @ManyToMany @JoinTable(name = "friends", joinColumns = @JoinColumn(name = "id2"), inverseJoinColumns = @JoinColumn(name = "id1")) private List<DemoEntity> friendOf;2,0171 gold badge10 silver badges20 bronze badges
Explore related questions
See similar questions with these tags.
