本文介绍了如何基于引用哪个实体来交换@JsonBackReference和@JsonManagedReference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据我从关联存储库引用的实体来交换@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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 03:39