在春季的Java中,特别是在JoinColumn中,如果我们使用JoinColumn例如JoinColumn(name =“ guest_id”)。然后将显示所有访客数据。而我只想检索一个数据,这只是名称。

@Column(name = "guest_id")
private Integer guestId;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "guest_id", insertable = false, updatable = false)
private String guestName;


当邮递员提出要求时,结果将是这样

    "guestCard": {
        "id": 53,
        "idPic": null,
        "guestType": null,
        "title": "mr",
        "name": "RIKO JANUAR",
        "phoneNumber": "08100000",
        "email": "[email protected]",
        "gender": "male",
        "bDay": "2019-10-29",
        "nationality": "AF",
        "idCard": "21321131231",
        "validity": "2019-10-29",
        "telpFax": null,
        "address": "bandung",
        "job": "Musician"
    }


我想要的是仅显示一个名称。像这样

 "guestCard": {
        "name": "RIKO JANUAR"
    }


我这样尝试过,但是没有用

@Column(name = "guest_id")
private Integer guestId;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "name.guest_id", insertable = false, updatable = false) //this is not work
private String guestName;

最佳答案

忽略要通过关系类上的@JsonIgnoreProperties jackson-annotations隐藏的所有字段。

其他解决方案,创建各种模型类并转移字段。

10-08 17:55