Reputation: 706
Can someone help me with this problem? I tried with mapstruct and it works just fine but only for entities which doesn't have bidirectional relationship.
For instance I have the entities:
@Entity
public class Pacients implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int pacientId;
// bi-directional many-to-one association to Doctori
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "doctorId")
private Doctors doctor;
//setters and getters
}
and
@Entity
public class Doctors implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int doctorId;
// bi-directional many-to-one association to Pacienti
@OneToMany(mappedBy = "doctor")
private List<Pacients> pacients;
//setters and getters
}
DTO's:
public class PacientsDto implements Serializable {
private int pacientId;
private Doctors doctor;
//setters and getters
}
public class DoctorsDto implements Serializable {
private int doctorId;
private List<Pacients> pacients;
//setters and getters
}
When I try to map them on dto's I get an StackOverflowError because of that birectional relationship.
Any idea how can I solve this? I will also accept a solution without using mapstruct.
If any details needed please let me know. Thank you!
Upvotes: 2
Views: 3294
Reputation: 18990
You'd have two mapping methods, one for mapping doctors and one for mapping patients. In the latter you advice the generator to ignore the reference to doctors. Then you can use an @AfterMapping
customization to set the doctors reference afterwards.
Upvotes: 3
Reputation: 96
Please use annotations JsonManagedReference for ManyToOne and JsonBackReference for OneToMany
Upvotes: 0