我创建了这样一个单向的一比一的关系:Hibernate的一对一允许多对一
@Entity(name="person")
public class Person {
@Id
@GeneratedValue
@Column(name = "pid", unique = true) // note 2
private int id;
// more fields
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="address_id", unique = true) // note 1
private Address address;
// rest of class
}
@Entity(name="address")
public class Address {
@Id
@GeneratedValue
@Column(name = "aid", unique = true)
private int id;
// rest of class
}
注1:
如果我不指定“ADDRESS_ID”列是唯一这样
@JoinColumn(name="address_id")
那么它是不是唯一的,我可以有很多的人指的是同一个地址是这样一个多到一的关系:
Person person1 = new Person();
Person person2 = new Person();
Address address = new Address("21th street");
person1.setAddress(address);
person2.setAddress(address);
不应该有一对一的关系使外键“address_id”在默认情况下是唯一的吗?
注2:
虽然我指定它的ID不是唯一的。为什么?
您是否尝试过在其中具有personId的地址,该地址需要是具有@ OneToOne/@ JoinColumn的Person类型? – ND27 2014-08-31 20:03:10
这不会使它成为单向的。 – Styl 2014-08-31 21:56:48