OneToMany 一端复合主键做多端的外键

一端有个复合主键,作为多端的外键,数据表如下:


代码:

复合主键的定义:

@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 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;

}

你可能感兴趣的:(OneToMany 一端复合主键做多端的外键)