【发布时间】:2016-09-26 12:07:01
【问题描述】:
有人可以帮我解决这个问题吗?我尝试使用 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 的解决方案。
如果需要任何详细信息,请告诉我。 谢谢!
【问题讨论】:
-
Java 和实体框架在同一个问题中。你确定吗?
-
你是对的。我将专注于如何提出问题,以便尽可能清晰。谢谢!
-
我假设您的 Dtos 应该相互引用,即
PacientsDto不应该引用Doctors而是引用DoctorsDto和DoctorsDto相同。