You are speaking about the situation where two entities share the same primary key. In this case of course there is no need for unique constraint, since the fk column is a primary column. But if the entities use their own keys, there MUST be a unique constraint.
If A points to B and the relation between is unidirectional OneToOne. In the database there will be a foreign key column (different than primary key column of TableA) in TableA pointing to the primary key column of TableB.
So, if I do
Code:
B b = new B();
A a1 = new A();
a1.setB(b);
A a2 = new A();
a2.setB(b);
and give them to the entity manager, there will be two records in TableA containing the same value of the B primary key from TableB, which will contain only one record corresponding to b.
If there is a unique constraint on the fk column in TableA, the situation above will not be possible. And this is exactly what OneToOne relation is: for every record in TableA there may be one or no record in TableB and otherwise.
So, there MUST be a unique constraint, otherwise it is not OneToOne.
And the JPA specification REQUIRES a unique constraint. And Hibernate does not generate any. Hibernate does not enforce a unique constraint if optional attribute of @OneToOne is true. So it is definitely a bug.
Extract from the spec:
2.10.3.1 Unidirectional OneToOne RelationshipsThe following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
Table A contains a foreign key to table B. The foreign key column name is formed as the con- catenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in table B. The foreign key column has the same type as the primary key of table B
and there is a unique key constraint on it.