我的 @Get
方法有问题。我有一个实体 ServcieChargeTier
,它与实体 CalendarEntry
具有 @OneToMany
关系。
问题是,当我尝试从服务器获取 ServiceChargeTier
时,服务器返回 ServiceChargeTier 的递归循环,其中包含 CalendarEntries
,每个 ServiceChargeTier 都有一个关联的 ServiceChargeTier,其中包含 CalendarEntries
等等。
我想返回每个 CalendarEntry 的 CalendarEntry,但不返回关联的 ServiceChargeTier。
ServiceChargeTier 映射:
public class ServiceChargeTier {
...
@OneToMany(mappedBy = "associatedServiceChargeTier", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
private List<CalendarEntry> calendarEntries = new ArrayList<>();
...
}
日历条目映射:
public class CalendarEntry {
...
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "service_charge_tier_id")
private ServiceChargeTier associatedServiceChargeTier;
...
}
当我发出获取 ServiceChargeTier 的请求时,它会返回如下 JSON:
[{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":{"id":40629,"calendarEntries":[{"id":40630,"associatedServiceChargeTier":
直到出现 stackOverFlow 错误。
最佳答案
由于这是一个双向关系, jackson 将在序列化另一个部分时继续序列化关系的每个部分,为了解决这个问题,您可以使用@JsonIngore
public class CalendarEntry {
...
@JsonIgnore
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "service_charge_tier_id")
private ServiceChargeTier associatedServiceChargeTier;
...
}
您还可以创建 DTO 并根据需要转换模型
关于java - JPA 获取连接实体返回递归获取循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45732174/