Reputation: 202
Why is that a bidirectional OneToOne
relationship behaves a bit weird in the following case?:
EntityManager
), and then reach the inverse side objectThe entities are nothing special:
Employee contains:
@OneToOne(cascade = CascadeType.ALL)
private Person person;
while Person contains:
@OneToOne(mappedBy="person")
private Employee employee;
It seems confusing for me, misleading. Is this a bug maybe, or the programmer has to know about their possibilities?
Upvotes: 0
Views: 29
Reputation: 3416
Why do you reuse the same Person
for two different employees? This is not a OneToOne
relationship anymore.
Btw what I suspect happens in the back, Hibernate executes the following query:
SELECT e FROM employee e WHERE person_id = ?
In this case the result set will contain two rows and Hibernate will use the first one only, of course the ordering is undefined in this case (depending on the DB you are using).
You can double check this by enabling SQL logging.
Upvotes: 1