Issue
@JsonIgnore doesn't work when a relationship is applient to the same entity but when used in the relationship with a different entity it works fine i want to use it to stop the recursive problem with json is there a way to fix this or using something different than @JsonIgnore and if someone know the answer please share with me the cause of the problem so i can avoid this in the future. Thank you in advance.
package com.socialapp.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(unique = true,name = "username")
private String Username;
@Column(unique = true,name = "mail")
private String mail;
@Column(name = "password")
private String password;
@JsonIgnore //this is a one to many relationship with entity post works fine
@OneToMany(mappedBy = "poster")
private Set<Post> posts= new HashSet<>();
@JsonIgnore //this is a many to many relationship with entity post works fine
@ManyToMany(mappedBy = "likers")
private Set<Post> postsLiked = new HashSet<>();
@ManyToMany
@JoinTable(
name = "follow",
joinColumns = @JoinColumn(name = "followers"),
inverseJoinColumns = @JoinColumn(name = "following")
)
private Set<User> Followers = new HashSet<>();
@JsonIgnore //this is a many to many relationship with the same entity @jsonignore doesn't work
@ManyToMany(mappedBy = "Followers")
private Set<User> Following = new HashSet<>();
public User(String username, String mail, String password) {
Username = username;
this.mail = mail;
this.password = password;
}
public User() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<Post> getPosts() {
return posts;
}
public void setPosts(Set<Post> posts) {
this.posts = posts;
}
public Set<Post> getPostsLiked() {
return postsLiked;
}
public void setPostsLiked(Set<Post> postsLiked) {
this.postsLiked = postsLiked;
}
public Set<User> getFollowers() {
return Followers;
}
public void setFollowers(Set<User> followers) {
Followers = followers;
}
public Set<User> getFollowing() {
return Following;
}
public void setFollowing(Set<User> following) {
Following = following;
}
public void addFollower(User following){
this.Followers.add(following);
}
}
"id": 1, "mail": "[email protected]", "password": "78824381", "followers": [ { "id": 2, "mail": "[email protected]", "password": "123456", "followers": [], "following": [ { "id": 1, "mail": "[email protected]", "password": "78824381", "followers": [ { "id": 2, "mail": "[email protected]", "password": "123456", "followers": [], "following": [ { "id": 1, "mail": "[email protected]", "password": "78824381", "followers": [ { "id": 2, "mail": "[email protected]", "password": "123456", "followers": [], "following": [ { "id": 1, "mail": "[email protected]", "password": "78824381", "followers": [ { "id": 2, "mail": "[email protected]", "password": "123456", "followers": [], "following": [ { "id": 1, "mail": "[email protected]", "password": "78824381", "followers": [ { "id": 2,
Solution
I think that your answer is here https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
Pay attention to paragraph 3and the annotations @JsonManagedReference and @JsonBackReference.
By using JsonManagedReference you can avoid the circular dependency issue.
Answered By - XII
Answer Checked By - David Goodson (JavaFixing Volunteer)