我有这些课:

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映射到两个Persons。

我在这里想念什么?

最佳答案

您必须添加@JoinColumn并将unique属性设置为true

10-04 22:56