首页 > 解决方案 > JPA - 复合键识别中的关系

问题描述

MST151:表结构

在此处输入图像描述

我想有一个如图所示的识别关系。

MST151:实体

@Entity
@Getter @Setter
@RequiredArgsConstructor
@Table(name = "MST151")
@Builder
public class MST151 {
    @Id
    @Column(name = "ITEM_ID")
    private String itemId;

    @Id
    @Column(name = "COMP_ID")
    private String compId;

    @Id
    @Column(name = "PRICE_REV")
    private int priceRev;

    @Id
    @Column(name = "PRICE_TYPE")
    private String priceType;

    @Column(name = "PRICE_STD")
    private double priceStd;

    @Column(name = "PRICE_UNIT")
    private String priceUnit;

    @Column(name = "DELIVERY_TYPE")
    private String deliveryType;

    @Column(name = "DELIVERY_DAY")
    private int deleveryDay;

    @Column(name = "DESCRIPTION")
    private String description;

    @Column(name = "CREATE_USER")
    private String createUser;

    @Column(name = "CREATE_TIME")
    private Date createTime;

    @Column(name = "UPDATE_USER")
    private String updateUser;

    @Column(name = "UPDATE_TIME")
    private Date updateTime;

    @Column(name = "USED")
    private int used;

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "ITEM_ID", insertable = false, updatable = false)
    private MST110 mst110;

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "COMP_ID", insertable = false, updatable = false)
    private MST150 mst150;

    public MST151(String itemId, String compId, int priceRev, String priceType, double priceStd, String priceUnit,
            String deliveryType, int deleveryDay, String description, String createUser, Date createTime,
            String updateUser, Date updateTime, int used, MST110 mst110, MST150 mst150) {
        super();
        this.itemId = itemId;
        this.compId = compId;
        this.priceRev = priceRev;
        this.priceType = priceType;
        this.priceStd = priceStd;
        this.priceUnit = priceUnit;
        this.deliveryType = deliveryType;
        this.deleveryDay = deleveryDay;
        this.description = description;
        this.createUser = createUser;
        this.createTime = createTime;
        this.updateUser = updateUser;
        this.updateTime = updateTime;
        this.used = used;
        this.mst110 = mst110;
        this.mst150 = mst150;
    }
}

MST150:实体

@Entity
@Getter @Setter
@RequiredArgsConstructor
@Table(name = "MST150")
@Builder
public class MST150 {
    @Id
    @Column(name = "COMP_ID")
    private String compId;

    @Column(name = "COMP_NAME")
    private String compName;

    @Column(name = "SALE_YN")
    private int saleYN;

    @Column(name = "PURCHASE_YN")
    private int purchaseYN;

    @Column(name = "OUTSOURCING_YN")
    private int outsourcingYN;

    @Column(name = "DOMESTIC_YN")
    private int domesticYN;

    @Column(name = "DESTRIBUTE_TYPE")
    private String destributeType;

    @Column(name = "BUSINESS")
    private String business;

    @Column(name = "INDUSTRY")
    private String industry;

    @Column(name = "REPRESENTATIVE")
    private String representative;

    @Column(name = "COMP_PHONE")
    private String compPhone;

    @Column(name = "COMP_FAX")
    private String compFax;

    @Column(name = "COMP_EMAIL")
    private String compEmail;

    @Column(name = "POST_NO")
    private String postNo;

    @Column(name = "ADDRESS")
    private String address;

    @Column(name = "HOMEPAGE")
    private String homepage;

    @Column(name = "MAIN_CHARGE")
    private String mainCharge;

    @Column(name = "CHARGE_PHONE")
    private String chargePhone;

    @Column(name = "CHARGE_CP")
    private String chargeCp;

    @Column(name = "CHARGE_FAX")
    private String chargeFax;

    @Column(name = "CHARGE_EMAIL")
    private String chargeEmail;

    @Column(name = "DESCRIPTION")
    private String description;

    @Column(name = "CREATE_USER")
    private String createUser;

    @Column(name = "CREATE_TIME")
    private Date createTime;

    @Column(name = "UPDATE_USER")
    private String updateUser;

    @Column(name = "UPDATE_TIME")
    private Date updateTime;

    @Column(name = "USED")
    private int used = 0;

    public MST150(String compId, String compName, int saleYN, int purchaseYN, int outsourcingYN, int domesticYN,
            String destributeType, String business, String industry, String representative, String compPhone,
            String compFax, String compEmail, String postNo, String address, String homepage, String mainCharge,
            String chargePhone, String chargeCp, String chargeFax, String chargeEmail, String description,
            String createUser, Date createTime, String updateUser, Date updateTime, int used) {
        super();
        this.compId = compId;
        this.compName = compName;
        this.saleYN = saleYN;
        this.purchaseYN = purchaseYN;
        this.outsourcingYN = outsourcingYN;
        this.domesticYN = domesticYN;
        this.destributeType = destributeType;
        this.business = business;
        this.industry = industry;
        this.representative = representative;
        this.compPhone = compPhone;
        this.compFax = compFax;
        this.compEmail = compEmail;
        this.postNo = postNo;
        this.address = address;
        this.homepage = homepage;
        this.mainCharge = mainCharge;
        this.chargePhone = chargePhone;
        this.chargeCp = chargeCp;
        this.chargeFax = chargeFax;
        this.chargeEmail = chargeEmail;
        this.description = description;
        this.createUser = createUser;
        this.createTime = createTime;
        this.updateUser = updateUser;
        this.updateTime = updateTime;
        this.used = used;
    }
}

MST110:实体

@Entity
@Table(name = "MST110")
@Getter
@Setter
@RequiredArgsConstructor
@Builder
public class MST110 {
    @Id
    @Column(name = "ITEM_ID")
    private String itemId;

    @Column(name = "ITEM_CODE")
    private String itemCode;

    @Column(name = "ITEM_NAME")
    private String itemName;

    @Column(name = "ITEM_TYPE")
    private String itemType;

    @Column(name = "LOT_SIZE")
    private double lotSize;

    @Column(name = "LOT_UNIT")
    private String lotUnit;

    @Column(name = "SAFETY_QNT")
    private double safetyQnt;

    @Column(name = "SAFETY_UNIT")
    private String safetyUnit;

    @Column(name = "LOC_CODE")
    private String locCode;

    @Column(name = "INV_TYPE")
    private int invType;

    @Column(name = "DESCRIPTION")
    private String description;

    @Column(name = "CREATE_USER")
    private String createUser;

    @Column(name = "CREATE_TIME")
    private Date createTime;

    @Column(name = "UPDATE_USER")
    private String updateUser;

    @Column(name = "UPDATE_TIME")
    private Date updateTime;

    @Column(name = "USED")
    private int used;

    @OneToOne(mappedBy = "mst110", fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "ITEM_ID", nullable = true)
    private MST111 mst111;

    @OneToOne(mappedBy = "mst110", fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "ITEM_ID", nullable = true)
    private MST112 mst112;

    @OneToOne(mappedBy = "mst110", fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "ITEM_ID", nullable = true)
    private MST113 mst113;

    @OneToOne(mappedBy = "mst110", fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "ITEM_ID", nullable = true)
    private MST114 mst114;

    public MST110(String itemId, String itemCode, String itemName, String itemType, double lotSize, String lotUnit,
            double safetyQnt, String safetyUnit, String locCode, int invType, String description, String createUser,
            Date createTime, String updateUser, Date updateTime, int used, MST111 mst111, MST112 mst112, MST113 mst113,
            MST114 mst114) {
        super();
        this.itemId = itemId;
        this.itemCode = itemCode;
        this.itemName = itemName;
        this.itemType = itemType;
        this.lotSize = lotSize;
        this.lotUnit = lotUnit;
        this.safetyQnt = safetyQnt;
        this.safetyUnit = safetyUnit;
        this.locCode = locCode;
        this.invType = invType;
        this.description = description;
        this.createUser = createUser;
        this.createTime = createTime;
        this.updateUser = updateUser;
        this.updateTime = updateTime;
        this.used = used;
        this.mst111 = mst111;
        this.mst112 = mst112;
        this.mst113 = mst113;
        this.mst114 = mst114;
    }
}

问题:我想映射 [MST151 结构] 之类的关联。但是,正如我所想,映射关联会产生错误。

在上述复合键标识结构中应该如何进行关联呢?

标签: javaspringhibernatejpa

解决方案


您可以使用如下嵌入式键:

@Embeddable
public class Mst151Key implements Serializable {
  @Column(name = "ITEM_ID")
  private String itemId;

  @Column(name = "COMP_ID")
  private String compId;

  @Column(name = "PRICE_REV")
  private int priceRev;

  @Column(name = "PRICE_TYPE")
  private String priceType;

}

public class MST151 {
    @EmbeddedId
    private Mst151Key mst151Key;

    @Column(name = "PRICE_STD")
    private double priceStd;

    @Column(name = "PRICE_UNIT")
    private String priceUnit;

    @Column(name = "DELIVERY_TYPE")
    private String deliveryType;

    @Column(name = "DELIVERY_DAY")
    private int deleveryDay;

    @Column(name = "DESCRIPTION")
    private String description;

    @Column(name = "CREATE_USER")
    private String createUser;

    @Column(name = "CREATE_TIME")
    private Date createTime;

    @Column(name = "UPDATE_USER")
    private String updateUser;

    @Column(name = "UPDATE_TIME")
    private Date updateTime;

    @Column(name = "USED")
    private int used;

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "ITEM_ID", insertable = false, updatable = false)
    private MST110 mst110;

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "COMP_ID", insertable = false, updatable = false)
    private MST150 mst150;
}

您可以在此处阅读有关此内容的更多信息


推荐阅读