Undeprecate session.update method #6068
Replies: 2 comments 8 replies
-
Yeah But we didn’t deprecate it because it’s redundant. We deprecated it because it’s bug-prone and something we simply don’t want people to use anymore in new code. (On the other hand, we’re not actually removing it yet.) |
Beta Was this translation helpful? Give feedback.
-
When I saw the |
Beta Was this translation helpful? Give feedback.
-
With Hibernate 6 we deprecated the
Session#update
and suggest to usemerge
instead.While investigating HHH-16111 I noticed some different behavior regarding
Optimistic Locking
and increase in theVersion
attribute, with particular regards to updates to the entity's relationships.The user's case consists basically in a parent entity with a
@OneToMany(mappedBy="...")
collection of child entities. The example he provided changes the collection of child entityes on a detached parent entity (by either adding/removing or modifying a child) and tries to reattach the state of the parent entity and its relationship. With Hibernate 5, he used to use theSession#update
methods which triggered an update of the parent entity regardless of changes to the child entities collection, thus increasing theVersion
attribute for optimistic locking.When upgrading to Hibernate 6, the user switched to using the
Session#merge
- as suggested by the@deprecated
javadoc - which actually behaves differently in this particular case: since the parent entity is the inverse side of the relationship, and no changes to the parent entity itself were made,merge
does not increase theVersion
attribute.It looks like this is in accordance to the JPA spec, which specifies (section 3.4.2. Version Attributes):
And defines the owning-side of a relationship (section 2.10. Entity Relationships) as:
In fact, removing
mappedBy
from theOneToMany
association (making the parent entity the owning side) does correctly trigger an increase of the parent's entityVersion
even when using themerge
method.In conclusion,
update
has a different behavior compared tomerge
: it forces an update of the detached entity's state, as opposed to first selecting the current state and only updating if needed, which affects howOptimistic Locking
occurs. For this reason maybe we should leave the option of using theSession#update
method to users who want to have this type of behavior.As an additional note, using
Session#update
withSelectBeforeUpdate
mapping - which is also deprecated - theVersion
increase does not happen because, like when usingmerge
, theVersion
of the parent is not increased since there was no change to its state. Also,SelectBeforeUpdate
is needed fororphanRemoval
to work correctly withupdate
, while merge handles it correctly.Beta Was this translation helpful? Give feedback.
All reactions