简言之,在表格中添加一栏“含有”与FK制约因素的关系。 例如:
CREATE TABLE MYENTITYA (
ID BIGINT NOT NULL,
MYENTITYB_ID BIGINT
);
CREATE TABLE MYENTITYB (
ID BIGINT NOT NULL
);
ALTER TABLE MYENTITYA ADD CONSTRAINT SQL100326144838300 PRIMARY KEY (ID);
ALTER TABLE MYENTITYB ADD CONSTRAINT SQL100326144838430 PRIMARY KEY (ID);
ALTER TABLE MYENTITYA ADD CONSTRAINT FKB65AC952578E2EA3 FOREIGN KEY (MYENTITYB_ID)
REFERENCES MYENTITYB (ID);
这将像以下那样:
@Entity
public class MyEntityA implements Serializable {
private Long id;
private MyEntityB myEntityB;
@Id
@GeneratedValue
public Long getId() {
return this.id;
}
@OneToOne(optional = true, cascade = CascadeType.ALL)
public MyEntityB getEntityB() {
return this.myEntityB;
}
//...
}
@Entity
public class MyEntityB implements Serializable {
private Long id;
@Id
@GeneratedValue
public Long getId() {
return id;
}
//...
}
如果<代码>EntityA和之间的关系> 而后添加“条码”限制。