0

在 Spring 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": "riko@email.com",
        "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;
4

2 回答 2

1

Ignore all fields you want to hide by @JsonIgnoreProperties jackson-annotations on the relationship class.

Other solution, create various model class and transfer the fields.

于 2019-11-19T08:46:53.453 回答
1

据我了解,您需要选择从实体中选择的内容。因此,创建一个 dto 并使用投影转换为查询结果。请参阅Spring 数据预测-

 @Projection(name="DtoProjection", types={Guest.class})
 public interface DtoOnly {

      Integer getGuestId();
      @Value("#{target.guestCard.guestName}")
      String getGuestName();
    }

另一种方法是在 HQL中使用Constructor

@Query("SELECT new NewDto(guest.guestID, guest.guestCard.guestName FROM Guest guest)")
    List<NewDto> findAllGuest();
于 2019-11-19T07:33:29.923 回答