我试图根据我从关联存储库引用的实体,找到一种交换@JsonBackRefence和@JsonManagedReference的方法。
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实体中。
注意: @JsonIdentityInfo 几乎可以处理它,但是它给了我太多信息,即:当我从BuildingRepository调用findById(buildingId)时,它为我提供了与找到的建筑物相连的站点的所有建筑物。
如果我对您的理解正确,那么@JsonIgnoreProperties注释应该可以帮助您:
@JsonIgnoreProperties
@JsonIgnoreProperties("site") @OneToMany(mappedBy="site") private List<Building> buildings; @JsonIgnoreProperties("buildings") @ManyToOne private Site site;