【发布时间】:2018-11-19 13:09:38
【问题描述】:
我有三个表,其中一个表使用其他两个表的外键来制作复合主键。现在我正在使用@Embeddable,但由于两个键都是外键,我现在将如何在实体中创建复合主键。
CREATE TABLE table1
(table1id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table1id));
表2
CREATE TABLE table2
(table2id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (table2id));
表3
CREATE TABLE table3 (
table1id INT NOT NULL,
table2id INT NOT NULL,
PRIMARY KEY (table1id, table2id),
FOREIGN KEY (table1id) REFERENCES table1 (table1id),
FOREIGN KEY (table2id) REFERENCES table2 (table2id)
);
如何将此表转换为 Hibenate 实体。
@Entity
@Table(name="table3")
class Table1 {
@Id
long table1id;
//getter and setter
}
@Entity
@Table(name="table3")
class Table2 {
@Id
long table2id;
//getter and Setter
}
@Entity
@Table(name="table3")
class Table3 {
@EmbeddedId
private table3PK table3PKId;
//getter and Setter
}
@Embeddable
Class table3PK{
@ManyToOne
@JoinColumn(name="table1Id" ,referencedColumnName="table1id")
Table1 table1;
@ManyToOne
@JoinColumn(name="table2Id" ,referencedColumnName="table2id")
Table2 table2;
public table3PK(){
}
public table3PK(Table1 table1 ,Table2 table2){
this.table1;
this.table2;
}
}
}
【问题讨论】:
标签: hibernate jpa spring-data-jpa