有人可以帮我解决这个问题吗?我尝试使用 mapstruct,它工作得很好,但仅适用于没有双向关系的实体。
例如我有实体:
@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
}
和
@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:
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
}
当我尝试将它们映射到 dto 时,由于这种双向关系,我得到了 StackOverflowError。
知道如何解决这个问题吗?我也会接受不使用 mapstruct 的解决方案。
如果需要任何详细信息,请告诉我。谢谢!