![](/img/trans.png)
[英]How to get started reading data from magnetic card with a simple bi-direction swipe magnetic card reader?
[英]ManyToOne OneToMany bi-direction: Retrieve data from reference table
我想同時發布一個對象,以從關系的引用表中返回數據。
關系定義如下:
@Entity
@Table( name = "application")
public class Application {
@Id
@JsonIgnore
private Long id;
@ManyToOne
@JoinColumn(name = "product_category")
private ProductCategory productCategory;
...
}
和:
@Entity
@Table(name = "product_category")
public class ProductCategory {
@Id
@Column(name = "product_category_id")
private Long productCategoryId;
@Column(name = "description")
private String description;
@JsonIgnore
@OneToMany(mappedBy = "productCategory")
private Set<Application> applications;
...
}
我的 product_category 表有以下數據:
使用以下結構發布我的 Json 對象時:
...
{
"productCategory": "0"
}
...
我想獲得以下 json 輸出:
...
"productCategory": {
"productCategoryId": 0,
"description": "Personal Loan"
},
...
但相反,我得到:
...
"productCategory": {
"productCategoryId": 0,
"description": null
},
...
你能給些建議么?
您的問題中沒有太多代碼,但您在評論中提到了您返回的內容:
return applicationRepository.save(appRequest);
另一方面,您不會在映射中將更改從父級級聯到子級:
@ManyToOne
@JoinColumn(name = "product_category")
private ProductCategory productCategory;
我認為將@ManyToOne
更改為@ManyToOne(cascade=CascadeType.ALL)
應該會有所幫助。
編輯:
如果您無法更改模型,只需返回您的實體:
return applicationRepository.findById(id);
這樣 jpa 不會在保存時覆蓋實體,它只是從 db 中讀取一個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.