I have the follwoing entity:
@Entity
public class A {
// id, etc..
@OneToOne
private B b;
}
The table a
already exists and when i add the new field b
to it, hibernate executes the following:
alter table a add column b_id int8
alter table a add constraint FKg76mxqt8whi8t8p4i7el95910 foreign key (b_id) references b
As you see, the foreign key column b_id
is not unique. Why is that the case? Doesn´t the One-to-One relation imply that the foreign key has to be unique? That´s also what i found in the JPA specification for unidirectional One-to-One relatoins:
[...] The foreign key column has the same type as the primary key of table B and there is a unique key constraint on it.
To make it work i have to explicitally add the @JoinColumn(unique=true)
annotation to the field. Why do i have to do that explicitally?