0

I'm trying to understand why a correct behaviour is happening:
I'm working with Hibernate 5.6 and I have a OneToMany bidirectional relationship.
All the following is happening in a transaction:

  • I recover with session.get an object which already exists in DB of the One Extreme.
  • I changed an int value in that object ( in the One extreme).
  • I create an object in memory of the Many extreme.
  • I associated both in memory.
  • I only save the object of the Many extreme and an insert for the Many extreme is created (correct) and an update is triggered for the object in the One extreme (correct too).

If no cascade is configured, why is it behaving like that without an extra saveOrUpdate explicit on One extreme?

If you could point me to a documentation link where this is explained I would appreciate it.

Thank you in advance.

Mary
  • 43
  • 5

1 Answers1

0

JPA specifies that in a bidirectional relationship, the @OneToOne/@OneToMany/@ManyToMany with a mappedBy specified is the "inverse" side which does not maintain the relationship. Only changes to the owning side @OneToOne/@ManyToOne/@ManyToMany (i.e. without mappedBy) will affect the relationship.

Read up on that in the JPA specification: https://jakarta.ee/specifications/persistence/3.1/jakarta-persistence-spec-3.1.html#a516

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58