Törpetestű
Törpetestű

Reputation: 202

OneToOne's mappedBy behaves weird

Why is that a bidirectional OneToOne relationship behaves a bit weird in the following case?:

  1. I create 2 owner side entities with the same inverse side entity
  2. With another run of the application, I find the second owner side entity (with EntityManager), and then reach the inverse side object
  3. And at this point if I reach the owner object through the inverse side object's reference, I get the first owner (attached picture helps)

enter image description here

The entities are nothing special:

It seems confusing for me, misleading. Is this a bug maybe, or the programmer has to know about their possibilities?

Upvotes: 0

Views: 29

Answers (1)

Arnold Galovics
Arnold Galovics

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

Related Questions