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

问题描述 投票:0回答:1

我有2代表父和子,父如1主键和复合唯一密钥(2根[UN_KEY1,UN_KEY2]的组合)。现在,在子表中我是指这2列作为外键。当我尝试生成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;
    }

}
java hibernate foreign-keys
1个回答
0
投票

以下内容添加到TblParent对象

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

当创建一个新的对象,你会做以下

TblParent p = new TblParent();
p.setSId(1);
p.setSomeCol1(12);
p.setUniqueKey1(12);
p.setUniqueKey2(14);
p.setTblChilds(new HashSet<TblChild>());

TblChild c = new TblChild();
c.setSId(1);
c.setSomeCol2(14);
c.setTblParent(p);
p.getTblChilds.add(c);

当你创建这个父对象的框架会自动外键值添加到TblChild表。

© www.soinside.com 2019 - 2024. All rights reserved.