gpt4 book ai didi

java - 如何同时添加响应id和加入对象

转载 作者:行者123 更新时间:2023-11-30 05:38:15 25 4
gpt4 key购买 nike

我想同时响应2个值:

  @Column(name = "category_id")
private int categoryId;

@OneToOne
@JoinColumn(name = "category_id")
private Category category;

第一个只是类别的 ID,第二个是该 ID 的嵌套对象。

{
"id": 1,
"productName": "Product HHYDP",
"categoryId": 1,
"unitInStock": 23,
"unitPrice": 18,
"category": {
"id": 1,
"categoryName": "Beverages",
"description": "Soft drinks, coffees, teas, beers, and ales",
"picture": null
}
}

最佳答案

您需要将此列标记为不可插入且不可更新。另一件事是为此属性实现 getter 作为类别 id 的返回值。

@Column(name = "category_id", insertable=false, updatable=false)
@JsonProperty(value="categoryId")
private int categoryId;

@OneToOne
@JoinColumn(name = "category_id")
private Category category;

public int getCategoryId(){
return category.getId();
}

关于java - 如何同时添加响应id和加入对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56211645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com