JPA unidirectional @OneToOne vs @ManyToOne with Hibernate - no difference?

According to Pro JPA 2, the main difference between the unidirectional @ManyToOne and @OneToOne is that in @OneToOne:

Only one instance of the source object can refer to the same instance of the target object. In other words, an instance of the target is not shared between instances of the source entities. In a database, this is equivalent to restricting uniqueness to the source column of the foreign key (that is, the column of the foreign key in the source entity table).

The thing is, when I create such an entity mapping and let Hibernate create the schema, there is no unique constraint created at all. What for? Because of this, for me there is no difference between @ManyToOne and @OneToOne if I have to explicitly define a unique restriction for display. I can do it for both of them, and it doesn't matter. Is this the right behavior?

+3
source share
2 answers

There is no reason to use OneToOne if the association is ManyToOne and vice versa. Use the appropriate annotation that reflects the reality of the power of the association. Do not do this only confused application developers, if not sleeping himself.

, Hibernate. , Hibernate OneToOne, , ( Hibernate , "n " ).

, , , -, - , .

+5

this equates to having a uniqueness constraint. , JPA . , .

a One to One a One to Many, . One to Many ​​( ) . One to One .

+3

All Articles