hibernatejpadb2外键

sqyvllje  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(187)

我有两个表parent和child,parent作为1个主键和一个复合唯一键(2列的组合[un\u key1,un\u key2])。现在在子表中,我将这两列称为外键。当我尝试在eclipse中生成实体时,它会显示多对一关系并显示父列。但是当我生成这两个列时,它们不会在父实体中生成。如何在没有这些列的情况下在这些实体中进行添加、更新、删除等事务
table

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

}
6rqinv9w

6rqinv9w1#

将以下内容添加到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表中。

相关问题