本文介绍了通过JPA GET连接的实体返回递归提取循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用@Get方法时遇到问题.我有一个与实体CalendarEntry@OneToMany关系的实体ServcieChargeTier.

I have a problem with a @Get method. I have an entity ServcieChargeTier which has a @OneToMany relationship with the entity CalendarEntry.

问题是,当我尝试从服务器获取ServiceChargeTier时,服务器返回ServiceChargeTier的递归循环,该循环具有CalendarEntries,每个循环都具有关联的ServiceChargeTier,并且具有CalendarEntries,依此类推上.

The problem is when I try and get a ServiceChargeTier from the server, the server returns a recursive loop of the ServiceChargeTier, which has CalendarEntries, which each has a ServiceChargeTier associated, which has the CalendarEntries and so on.

我想为每个CalendarEntry返回CalendarEntry的,而不是相关的ServiceChargeTier.

I would like to return the CalendarEntry's but not the associated ServiceChargeTier for each CalendarEntry.

ServiceChargeTier映射:

ServiceChargeTier Mapping:

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:

When I make a request to get a ServiceChargeTier it returns a JSON like this:

[{"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错误.

until it gives a stackOverFlow error.

推荐答案

由于这是双向关系,因此杰克逊将在序列化另一个关系时继续对关系的每个部分进行序列化,要解决此问题,您可以使用@JsonIngore

Since this is a bidirectional relation jackson will keep serializing each part of the relation when serializing the other, to solve it you can use @JsonIngore

public class CalendarEntry {

...
@JsonIgnore
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "service_charge_tier_id")
private ServiceChargeTier associatedServiceChargeTier;

...

}

您还可以创建DTO并根据需要转换模型

you could also create a DTO and convert your model as you want

这篇关于通过JPA GET连接的实体返回递归提取循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 04:52