我有这些课程:
public class Person {
@Id
private int personId;
@OneToOne
private SomeOtherId someOtherId;
}
public class SomeOtherId {
@Id
private int someId;
// other fields
}
我这样做了:
Person person1 = new Person(1);
SomeOtherId someOtherId = new SomeOtherId(100);
person1.setSomeOtherId(someOtherId);
//Update the database so that both the above entities are persisted
Person person2 = new Person(2);
person2.setSomeOtherId(someOtherId);
//Update the database so that person2 is persisted
一切正常!并且它将someOtherId分配给它不应该的人实体,因为关系是OneToOne。每个人都必须拥有一个独特的SomeOtherId。在数据库中,我可以看到SomeOtherId的单个实体和映射到两个人的id。
我在这里缺少什么?
答案 0 :(得分:1)
您必须添加@JoinColumn并将unique属性设置为true