问题描述
我正在尝试根据我从关联存储库中引用的实体找到一种方法来交换 @JsonBackRefence 和 @JsonManagedReference.
I'm trying to find a way to swap the @JsonBackRefence and the @JsonManagedReference based on what entity I reference from the associated repository.
Site.java
@Entity
@Table(name = "Site")
public class Site {
@Id
private String id;
@OneToMany(mappedBy="site")
@JsonManagedReference
private List<Building> buildings;
}
Building.java
@Entity
@Table(name = "building")
public class Building{
@Id
private String id;
@ManyToOne
@JoinColumn(name = "SITE_ID")
@JsonBackReference
private Site site;
}
SiteRepository.java
public List<Site> findAll(); //Works as intended
BuildingRepository.java
public Building findById(buildingId); //Works if references are swapped
但是,在调用 findById(buildingId) 时,我希望交换 @JsonBackReference.因此,@JsonBackReference 在 Site.java 中,@JsonManagedReference 在 Building.java 实体中.
However when calling findById(buildingId), I want to have the @JsonBackReference swapped. Therefore, the @JsonBackReference is in Site.java and the @JsonManagedReference is in the Building.java entity.
注意:@JsonIdentityInfo 几乎可以处理它,但它给了我太多信息,即:当我从 BuildingRepository 调用 findById(buildingId) 时,它给了我与建筑物相连的站点的所有建筑物找到了.
Note: @JsonIdentityInfo almosts handles it, but it gives me too much information ie: when I call findById(buildingId) from the BuildingRepository it gives me all the buildings for the site joined to the building found.
推荐答案
如果我理解正确,@JsonIgnoreProperties
注释应该对您有所帮助:
If I understand you correctly the @JsonIgnoreProperties
annotation should help you:
@JsonIgnoreProperties("site")
@OneToMany(mappedBy="site")
private List<Building> buildings;
@JsonIgnoreProperties("buildings")
@ManyToOne
private Site site;
这篇关于如何根据引用的实体交换@JsonBackReference 和@JsonManagedReference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!