@OneToMany关系中的实现问题,递归调用导致无限循环。

huangapple go评论78阅读模式
英文:

implementation issue on @OneToMany relationship recursive call infinite loop

问题

我有两个类别,城市和宗教场所,一个城市可以拥有多个宗教场所,但一个宗教场所只能属于一个城市,但按要求实施时,我遇到了一个错误,我的响应卡在递归调用中。

我期望得到所请求城市中所有宗教场所的响应。

英文:

I've two classes city and religious places where a city can have multiple religious places, but a religious place can have only one city, but when I implement as per requirement I got an error my response stuck in the recursive call.

I am expecting response of all religious places which is located in requested city

答案1

得分: 1

你可以通过在City类的religious places字段和ReligiousPlace类的city字段上添加@JsonIgnore来告诉Jackson在序列化期间忽略特定数据。

@Entity
@Table(name = "religious_places")
class ReligiousPlace {

    @ManyToOne
    @JoinColumn(name = "city_id")
    @JsonIgnore
    private City city;

}

@Entity
@Table(name = "city")
class City {

    @OneToMany(mappedBy = "city", cascade = CascadeType.ALL)
    @JsonIgnore
    private List<ReligiousPlace> religious_places;
}
英文:

You may tell Jackson to disregard certain data during serialisation by adding @JsonIgnore to the religious places field in the City class and the city field in the ReligiousPlace class.

@Entity
@Table(name = &quot;religious_places&quot;)
class ReligiousPlace {

	@ManyToOne
    @JoinColumn(name = &quot;city_id&quot;)
    @JsonIgnore
    private City city;
	
}

@Entity
@Table(name = &quot;city&quot;)	
class City {

	@OneToMany(mappedBy = &quot;city&quot;, cascade = CascadeType.ALL)
    @JsonIgnore
    private List&lt;ReligiousPlace&gt; religious_places;
}

huangapple
  • 本文由 发表于 2023年6月30日 01:45:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定