[英]Unique constraint in OneToOne bi-directional mapping
Is unique=true
required in bi-directional one-to-one mapping in the owner of the relationship? 关系所有者中的双向一对一映射是否需要
unique=true
?
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
@OneToOne(cascade={CascadeType.PERSIST})
@JoinColumn(name="passport_id", unique=true) //is unique=true required for bi-directional one-to-one mapping
private Passport passport;
public Passport getPassport() {
return passport;
}
}
@Entity
public class Passport {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(name="passport_number")
private String passportNumber;
@OneToOne(mappedBy="passport")
private Customer customer;
public Customer getCustomer() {
return customer;
}
}
Hibernate documentation says that FK column in the database should be constrained unique to simulate one-to-one multiplicity , but it doesn't add the unique=true
in the bi-directional mapping. Hibernate文档说数据库中的FK列应被约束为唯一,以模拟一对一的多重性 ,但它不会在双向映射中添加
unique=true
。
That's because it's not mandatory to use the Hibernate auto DDL feature. 这是因为使用Hibernate自动DDL功能不是强制性的。 You can have incremental schema update scripts and the schema related annotations would be useless.
您可以使用增量模式更新脚本,并且与模式相关的注释将无用。 Frankly, we kinda use those for in memory integration testing.
坦白地说,我们在内存集成测试中使用了那些。
As you pointed out, the JoinColumn should state the uniqueness constraint. 如您所指出的,JoinColumn应该声明唯一性约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.