Spring:获取 ManyToOne 实体时,参考实体 (OneToMany) 未显示在 JSON 中 [英] Spring: When getting a ManyToOne entity, reference entity (OneToMany) is not showing in JSON

查看:18
本文介绍了Spring:获取 ManyToOne 实体时,参考实体 (OneToMany) 未显示在 JSON 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 POSTMAN 中发送 GET 请求以获取我所有的子实体(城镇)时,父实体(省)未显示在 JSON 响应中.

When I send a GET request in POSTMAN to get all my child entity (Town) the parent entity (Province) is not shown in the JSON response.

这是我的控制器.

@RequestMapping(value ="api/v1/town",method = RequestMethod.GET)
public ResponseEntity<List<Town>> getAllTowns() {
    List<Town> towns = townService.getAllTowns();
    if(towns.isEmpty()) {
        return new ResponseEntity<List<Town>>(HttpStatus.NO_CONTENT);
    }

    return new ResponseEntity<List<Town>>(towns, HttpStatus.OK);
}

这些是我的实体.

父类

@Entity
@Table(name = "PROVINCE")
public class Province {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "PROVINCE_ID")
    private long id;

    private String name;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "province", targetEntity = Town.class)
    @JsonManagedReference("Province-Town")
    private List<Town> towns;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Town> getTowns() {
        return towns;
    }

    public void setTowns(List<Town> towns) {
        this.towns = towns;
    }
}

子类

@Entity
@Table(name = "TOWN")
public class Town {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "TOWN_ID")
    private long id;

    private String name;

    @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name = "PROVINCE_ID")
    @JsonBackReference("Province-Town")
    private Province province;

    private long kilometer;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Province getProvince() {
        return province;
    }

    public void setProvince(Province province) {
        this.province = province;
    }

    public long getKilometer() {
        return kilometer;
    }

    public void setKilometer(long kilometer) {
        this.kilometer = kilometer;
    }
}

我得到的回应是这样的

{
    "id" : 1,  
    "name" : "Some Town",
    "kilometer" : 350
}

我期待的是

{
    "id" : 1,  
    "name" : "Some Town",
    "province" : { 
             //Province data.....
     }
    "kilometer" : 350
}

我能够展示这样的东西,但我使用的对象不是 Spring-data-jpa 实体,只是简单的 POJO.

I was able to show something like this, but the Objects that I used are not Spring-data-jpa entities, just simple POJOs.

我的实体有问题吗?或者还有什么别的吗?

Is there any problem with my Entities? Or is there anything else?

推荐答案

交换 @JsonBackReference@JsonManagedReference.基本上:

Swap @JsonBackReference and @JsonManagedReference. Basically:

@JsonManagedReference
private Province province;

@JsonBackReference
private List<Town> towns;

这篇关于Spring:获取 ManyToOne 实体时,参考实体 (OneToMany) 未显示在 JSON 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆