![](/img/trans.png)
[英]insert record with JPA having a field as an foreign key to another table
[英]JPA foreign key - insert new record if necessary?
是否可以創建注釋的某種組合,從而提供以下內容:
否。您將需要自行創建所有對象。
您可以根據需要使用級聯執行自動持久化。
就像這樣:
a.setB(b);
b.getAList().add(a);
entityManager.persist(a);
在您的課程中,您將映射為:
public class A {
// other methods and ID
@ManyToOne
@JoinColumn(name = "b_id")
private B b;
}
public class B {
// other methods and ID
@OneToMany(mappedBy = "b", cascade = CascadeType.PERSIST)
private List<A> aList;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.