As we know:
一对一关联有三种情况:(1).关联的实体都共享同样的主键,(2).其中一个实体通过外键关联到另一个实体的主键(注意要模拟一对一关联必须在外键列上添加唯一约束),(3).通过关联表来保存两个实体之间的连接关系(要模拟一对一关联必须在每一个外键上添加唯一约束)。
For Type 1: Here is the example
@Entity
@Table(name="Test_Body")
public class Body {
private Integer id;
private Heart heart;
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne
@PrimaryKeyJoinColumn
public Heart getHeart() {
return heart;
}
public void setHeart(Heart heart) {
this.heart = heart;
}
}
@Entity
@Table(name="Test_Heart")
public class Heart {
private Integer id;
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
通过@PrimaryKeyJoinColumn批注定义了一对一关联
We get two tables:
Test_Body:
id
Test_Heart
id
Those two tables have just only one column "id", no others else. Is that wrong ?
So question is why we should use this sort of mapping ? In what sort of cases we need use this map ?