我正在尝试使用共享主键实现一对一的单向映射...(ref ... https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-association)
我使用的db结构如下,
Table Name : HUMANBODY
Columns : ID , NAME
Table Name : HEART
Columns : ID , NAME
实体映射是,
@Entity
@Table(name = "humanbody")
public class HumanBody {
@Id
@SequenceGenerator(name = "bodygen", sequenceName = "humanbody_swq")
@GeneratedValue(generator = "bodygen")
private int id;
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Heart getHeart() {
return heart;
}
public void setHeart(Heart heart) {
this.heart = heart;
}
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn(name="ID", referencedColumnName="ID")
private Heart heart;
}
@Entity
@Table(name = "heart")
public class Heart {
@Id
private int id;
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我面临的问题是,如果我保存了人体实体,主键未在心脏实体中正确填充,它会在HEART表的数据库中将主键更新为0。
以下是用于保存人体实体的代码段。
HumanBody body = new HumanBody();
body.setName("human1");
Heart heart = new Heart();
heart.setName("human1heart");
body.setHeart(heart);
session.save(body);
不确定我在做什么错?有谁可以帮忙解决这个问题?...