Java 如何同时添加响应id和连接对象
我想同时响应2个值:Java 如何同时添加响应id和连接对象,java,spring,spring-boot,Java,Spring,Spring Boot,我想同时响应2个值: @Column(name = "category_id") private int categoryId; @OneToOne @JoinColumn(name = "category_id") private Category category; 第一个是category的ID,第二个是这个ID的嵌套对象 { "id": 1, "productName": "Product HHYDP", "categoryId": 1, "uni
@Column(name = "category_id")
private int categoryId;
@OneToOne
@JoinColumn(name = "category_id")
private Category 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作为category 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();
}
实体od categoryId中的映射是不必要的,因为它等于category.id。你可以去掉这个。