java - JPA 获取连接实体返回递归获取循环

标签 java entity-framework hibernate jpa recursion

我的 @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/

相关文章:

java - 如何只修剪字符串开头的空格

c# - 无法跟踪实体类型 'xTestType' 的实例,因为已跟踪具有相同键的该类型的另一个实例?

asp.net - 在 ASP.Net 上每个 Web 请求创建 dbcontext 的成本

java - Hibernate Criteria AND OR 对多个 where 的操作

java - JavaMail : PKIX path building failed unable to find valid certification path to requested target 中的错误

java - 无法使用外部库

c# - 将 ASP.NET Identity 集成到现有的 DbContext

java - hibernate查询语言还是使用条件?

java - 如何在 hibernate 中获取排序结果中的第一组结果?

java - Do Loop变量无法解析为变量