Reputation: 4524
An employer has several employees, so employees have (many-to-one) relations with their employer. Provided that we ignore reverse relation (one-to-many)from employer to the employee, then from the employee perspective, this can be represented as a one-to-one relation !?
Upvotes: 4
Views: 986
Reputation: 2565
At first glance it might appear so - you won't get any errors when using a @OneToOne
relation when it's in fact a @ManyToOne
and in most cases hibernate won't behave any differently since as far as the owning entity (the employee in your example) is concerned it only cares about itself and the non-owning entity (the employer) and doesn't need to know about its siblings (the other employees) BUT @OneToOne
has additional properties that @ManyToOne
doesn't since it ASSUMES (seeing as it's a one-to-one) that it has a direct relationship with the other side of the relation:
@OneToOne
has an orphanRemoval
property which allows you to delete the other side of the relation (employer) if the owning entity (employee) is deleted - this is not available with @ManyToOne
There are also more direct differences in the Hibernate layer, where if you reference the SAME non-owning entity (employee) from different owning entities (employer) within the same transaction you will get an exception when using @OneToOne
with a relationship that should be @ManyToOne
- see this answer for an example: https://stackoverflow.com/a/18463748/3368558
Upvotes: 6