JPA unidirectional @OneToOne vs @ManyToOne with Hibernate - no difference?
hibernate, jpa, many-to-one, one-to-one
Solution
There's no reason to use OneToOne if the association is a ManyToOne, and vice-versa. Use the appropriate annotation, which reflects the reality of the cardinality of the association. Not doing so would only confuse the developers of the app, if not Hibernate itself.
Whether the uniqueness of the source is constrained in the database or not doesn't change anything for Hibernate. In my experience, Hibernate does create the unique constraint in case of a OneToOne, and if it doesn't, then you should create it (I wouldn't use Hibernate to create the schema anyway, except for a quick 'n dirty demo app).
But of course, if there is a unique constraint, and you try to create two different entities with the same target entity, that will fail due to an error thrown by the database unique constraint.
Problem
According to book Pro JPA 2 the main difference between unidirectional @ManyToOne and @OneToOne is that in @OneToOne: Only one instance of the source entity can refer to the same target entity instance. In other words, the target entity instance is not shared among the source entity instances. In the database, this equates to having a uniqueness constraint on the source foreign key column (that is, the foreign key column in the source entity table). The thing is, when I create such a mapping on entity and let Hibernate create schema, there is no unique constrain created at all. Why? Because of that for me, there is no difference between @ManyToOne and @OneToOne if I must explicitly define unique constraint for the mapping. I can do it for both of them and it makes no difference. Is that correct behaviour?