我是新來的JPA,所以我有大約多對多的關係(以零到更多實現)一個問題:JPA多對多的關係:零個或多個
如果你有這樣的關係:
就像說一個產品可以不用爲了excist(通常它們會被新產品到貨加)以後它可以在1個或多個訂單中使用。訂單必須至少包含一個或多個產品。
,您必須註明的關係
@Entity(name = "ORDERS")
public class Order {
@Id
@Column(name = "ORDER_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long orderId;
@Column(name = "CUST_ID")
private long custId;
@Column(name = "TOTAL_PRICE", precision = 2)
private double totPrice;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="ORDER_DETAIL",
joinColumns=
@JoinColumn(name="ORDER_ID", referencedColumnName="ORDER_ID"),
inverseJoinColumns=
@JoinColumn(name="PROD_ID", referencedColumnName="PROD_ID")
)
private List<Product> productList;
...............
The other attributes and getters and setters goes here
}
和
@Entity(name = "PRODUCT")
public class Product {
@Id
@Column(name = "PROD_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long prodId;
@Column(name = "PROD_NAME", nullable = false,length = 50)
private String prodName;
@Column(name = "PROD_DESC", length = 200)
private String prodDescription;
@Column(name = "REGULAR_PRICE", precision = 2)
private String price;
@Column(name = "LAST_UPDATED_TIME")
private Date updatedTime;
@ManyToMany(mappedBy="productList",fetch=FetchType.EAGER)
private List<Order> orderList;
...............
The other attributes and getters and setters goes here
}
我不知道零到很多關係仍然可能只是堅持誰沒有鏈接(目前),以產品訂單?
但是,當訂單使用產品時,產品中的訂單清單應該更新,產品清單也應該更新。我如何執行此操作或爲我執行JPA操作?
我在這裏生鏽。在連接表中的記錄實際創建之前,是否有必要更新manytomany兩側的集合? – Gimby
我以爲是一樣的,因此我的問題:) –