一端有個(gè)復(fù)合主鍵贴彼,作為多端的外鍵暖混,數(shù)據(jù)表如下:
代碼:
復(fù)合主鍵的定義:
@Embeddable
public class SupplierArticlePK
implements Serializable {
private int supplierId;
private String supplierArticleId;
}
一端的定義:
@Entity
@Table( name = "supplier_article" )
public class SupplierArticle {
@Id
private SupplierArticlePK supplierArticlePK;
@Enumerated( EnumType.STRING )
private BusinessModel businessModel;
@OneToMany( cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER )
@JoinColumns( {
@JoinColumn( name = "parentSupplierId", referencedColumnName = "supplierId" ),
@JoinColumn( name = "parentSupplierArticleId", referencedColumnName = "supplierArticleId" )
} )
private Set<PurchasePrice> purchasePrices = new HashSet<>();
private int forwardingAgency;
}
多端的定義:
@Entity
@Table( name = "purchase_price" )
public class PurchasePrice {
@ManyToOne
@JoinColumns( {
@JoinColumn( name = "parentSupplierId", referencedColumnName = "supplierId" ),
@JoinColumn( name = "parentSupplierArticleId", referencedColumnName = "supplierArticleId" )
} )
public SupplierArticle supplierArticle; //unidirectional
@Id
@GeneratedValue
private int id;
@Enumerated( EnumType.STRING )
private SalePriceType salePriceType;
@Temporal( TemporalType.DATE )
private Date validFromDate;
@Temporal( TemporalType.DATE )
private Date validToDate;
private BigDecimal value;
private String currency;
}