基于复合唯一密钥的Hibernate JPA DB2外键

时间:2019-02-04 14:02:08

标签: java hibernate foreign-keys

我有2个表父级和子级,父级作为1个主键和一个复合唯一键(2列的组合[UN_KEY1,UN_KEY2])。现在在子表中,我将这两列称为外键。当我尝试在eclipse中生成实体时,它显示多对一关系并显示父列。但是当我生成这两个列时,不会在父实体中生成。在没有这些列的情况下如何在这些实体中进行添加,更新,删除之类的交易

表格

--drop table  "TBL_PARENT";

CREATE TABLE "TBL_PARENT"(
  "S_ID" INTEGER NOT NULL  GENERATED ALWAYS AS IDENTITY(START WITH 1, INCREMENT BY 1, NO MINVALUE, NO MAXVALUE, CACHE 20),
  "UN_KEY1" Integer NOT NULL,
  "UN_KEY2" Smallint NOT NULL,
  "SOME_COL1" Integer
);

-- Add keys for table TBL_PARENT

ALTER TABLE "TBL_PARENT" ADD CONSTRAINT "TBL_PARENTKEY3" PRIMARY KEY ("S_ID");

ALTER TABLE "TBL_PARENT" ADD CONSTRAINT "TBL_PARENTKey4" UNIQUE ("UN_KEY1","UN_KEY2");

--drop table  "TBL_PARENT";

CREATE TABLE "TBL_CHILD"(
  "S_ID" Integer NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1, INCREMENT BY 1, NO MINVALUE, NO MAXVALUE, CACHE 20),
  "UN_KEY1" Integer,
  "UN_KEY2" Integer,
  "SOME_COL2" Integer
  );


ALTER TABLE "TBL_CHILD" ADD CONSTRAINT "TBL_CHILD_KEY3" PRIMARY KEY ("S_ID");

ALTER TABLE "TBL_CHILD" ADD CONSTRAINT "TBL_CHILD_FK" FOREIGN KEY ("UN_KEY1", "UN_KEY2") REFERENCES "TBL_PARENT" ("UN_KEY1", "UN_KEY2");

生成的代码:

@Entity
@Table(name="TBL_PARENT")
@NamedQuery(name="TblParent.findAll", query="SELECT t FROM TblParent t")
public class TblParent implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="S_ID")
    private int sId;

    @Column(name="SOME_COL1")
    private int someCol1;

    //bi-directional many-to-one association to TblChild
    @OneToMany(mappedBy="tblParent")
    private Set<TblChild> tblChilds;

    public TblParent() {
    }

    public int getSId() {
        return this.sId;
    }

    public void setSId(int sId) {
        this.sId = sId;
    }

    public int getSomeCol1() {
        return this.someCol1;
    }

    public void setSomeCol1(int someCol1) {
        this.someCol1 = someCol1;
    }

    public Set<TblChild> getTblChilds() {
        return this.tblChilds;
    }

    public void setTblChilds(Set<TblChild> tblChilds) {
        this.tblChilds = tblChilds;
    }

    public TblChild addTblChild(TblChild tblChild) {
        getTblChilds().add(tblChild);
        tblChild.setTblParent(this);

        return tblChild;
    }

    public TblChild removeTblChild(TblChild tblChild) {
        getTblChilds().remove(tblChild);
        tblChild.setTblParent(null);

        return tblChild;
    }

}

@Entity
@Table(name="TBL_CHILD")
@NamedQuery(name="TblChild.findAll", query="SELECT t FROM TblChild t")
public class TblChild implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="S_ID")
    private int sId;

    @Column(name="SOME_COL2")
    private int someCol2;

    //bi-directional many-to-one association to TblParent
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name="UN_KEY1", referencedColumnName="UN_KEY1"),
        @JoinColumn(name="UN_KEY2", referencedColumnName="UN_KEY2")
        })
    private TblParent tblParent;

    public TblChild() {
    }

    public int getSId() {
        return this.sId;
    }

    public void setSId(int sId) {
        this.sId = sId;
    }

    public int getSomeCol2() {
        return this.someCol2;
    }

    public void setSomeCol2(int someCol2) {
        this.someCol2 = someCol2;
    }

    public TblParent getTblParent() {
        return this.tblParent;
    }

    public void setTblParent(TblParent tblParent) {
        this.tblParent = tblParent;
    }

}

1 个答案:

答案 0 :(得分:0)

将以下内容添加到TblParent对象

private void setDataToList() {
    if (getArguments() != null) {
        String newText = getArguments().getString("Check");
        simpleway.toastMessage(mContext, newText);
    } else {
        simpleway.toastMessage(mContext, "Failed");
    }

}

创建新对象时,您将执行以下操作

@Column(name = "UN_KEY1")
private int uniqueKey1;
@Column(name = "UN_KEY2")
private int uniqueKey2;

创建此父对象时,框架将自动将外键值添加到TblChild表中。