Java 获取连接实体的JPA返回递归获取循环
我对Java 获取连接实体的JPA返回递归获取循环,java,entity-framework,hibernate,jpa,recursion,Java,Entity Framework,Hibernate,Jpa,Recursion,我对@Get方法有问题。我有一个实体ServcieChargeTier,它与实体日历条目有@OneToMany关系 问题是,当我尝试从服务器获取ServiceChargeTier时,服务器返回ServiceChargeTier的递归循环,其中包含CalendarEntries,每个都有一个关联的ServiceChargeTier,其中包含CalendarEntries,依此类推 我想返回CalendarEntry,但不返回每个CalendarEntry的关联ServiceChargeTier S
@Get
方法有问题。我有一个实体ServcieChargeTier
,它与实体日历条目有@OneToMany
关系
问题是,当我尝试从服务器获取ServiceChargeTier
时,服务器返回ServiceChargeTier的递归循环,其中包含CalendarEntries
,每个都有一个关联的ServiceChargeTier,其中包含CalendarEntries
,依此类推
我想返回CalendarEntry,但不返回每个CalendarEntry的关联ServiceChargeTier
ServiceChargeTier映射:
public class ServiceChargeTier {
...
@OneToMany(mappedBy = "associatedServiceChargeTier", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
private List<CalendarEntry> calendarEntries = new ArrayList<>();
...
}
当我请求获取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并根据需要转换模型,因为这是一个双向关系,jackson在序列化其他部分时会不断序列化关系的每个部分,要解决这个问题,您可以使用@JsonIngore
public class CalendarEntry {
...
@JsonIgnore
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "service_charge_tier_id")
private ServiceChargeTier associatedServiceChargeTier;
...
}
您还可以创建DTO并根据需要转换模型序列化为JSON时可以排除该字段。如果您使用Jackson,请检查:这与jpaapi无关,而与JSON有关。它们是完全不同的进程…@Thoomas谢谢,我会仔细阅读。@NeilStockton是的,我的错。当序列化为JSON时,您可以排除该字段。如果您使用Jackson,请检查:这与jpaapi无关,而与JSON有关。它们是完全不同的过程…@Thomas谢谢,我会仔细阅读。@NeilStockton是的,我的错。是的,这就是我想要的。谢谢!:)是的,这就是我想要的。谢谢!:)