我从这里扩展我的问题:Define CompositeKey with three tables using JPA/Hibernate? . 在此示例中,我希望创建复合键以创建 PRODUCT_ID、CATEGORY_ID、STOCK_ID 的唯一组合。
我开发了以下代码,但不确定如何将记录保存到数据库中。
Stock.java
@Entity
public class Stock implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "STOCK_ID", unique = true, nullable = false)
private Integer stockId;
@Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
private String stockCode;
@Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
private String stockName;
// Owner of the relationship
@OneToMany(fetch = FetchType.LAZY, mappedBy = "linkPk.stock", cascade = CascadeType.ALL)
private Set<StockCategoryProductLink> stockCategoryProductLinks = new HashSet<>(0);
public Stock() {
}
public Stock(Integer stockId, String stockCode, String stockName,
Set<StockCategoryProductLink> stockCategoryProductLinks) {
super();
this.stockId = stockId;
this.stockCode = stockCode;
this.stockName = stockName;
this.stockCategoryProductLinks = stockCategoryProductLinks;
}
public Integer getStockId() {
return stockId;
}
public void setStockId(Integer stockId) {
this.stockId = stockId;
}
public String getStockCode() {
return stockCode;
}
public void setStockCode(String stockCode) {
this.stockCode = stockCode;
}
public String getStockName() {
return stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
public Set<StockCategoryProductLink> getStockCategoryProductLinks() {
return stockCategoryProductLinks;
}
public void setStockCategoryProductLinks(Set<StockCategoryProductLink> stockCategoryProductLinks) {
this.stockCategoryProductLinks = stockCategoryProductLinks;
}
}
Qyouu
相关分类