JPA onetomany mapping showing nested data many times

Viewed 20

I have two table user(id,name) and user_mails(id,email,user_id) user to user_mails have one to many relation.

I have created following entity in spring boot

User

  @Entity
  @Table(name = "user")
  public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;

@Column(name = "name", nullable = false)
private String name;


 @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<UserMail> userMails =new HashSet<UserMail>(0);
 //Getter setter and constructor
}

UserMail

  @Entity
  @Table(name = "user_mails")

  public class UserMail {
   @Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Column(name = "email", nullable = false)
private String name;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id")
private User user;

It is showing following output on calling controller

     [{"id":1,"name":"Ram","userMails":[{"id":2,"name":"ram@b.com","user":{"id":1,"name":"Ram","userMails":[{"id":2,"name":"ram@b.com","user":{"id":1,"name":"Ram","userMails":[{"id":2,"name":"ram@b.com","user":{"id":1,"name":"Ram","userMails":[{"id":2,"name":"ram@b.com","user":{"id":1,"name":"Ram","userMails":[{"id":2,"name":"ram@b.com","user":{"id":1,"name":"Ram","userMails":[{"id":2,"name":"ram@b.com","user":{"id":1,"name":"Ram","userMails":

and more

I want to access all users with all mail ids also want to acces mail id with user details

What changes should I do to get proper result

0 Answers
Related