FWIW saveOrUpdate()
looks like the best option overall:
The saveOrUpdate() method is in practice more useful than
update()
,save()
, orlock()
: In complex conversations, you don’t know if the item is in detached state or if it’s new and transient and must be saved. The automatic state-detection provided bysaveOrUpdate()
becomes even more useful when you not only work with single instances, but also want to reattach or persist a network of connected objects and apply cascading options.
However for your case, if you are sure the entity was modified in detached state, and/or don't mind occasionally hitting the DB with an unnecessary UPDATE, maybe update()
is the safest choice:
The
update()
operation on the Session reattaches the detached object to the persistence context and schedules an SQLUPDATE
. Hibernate must assume that the client modified the object while it was detached. [...] The persistence context is flushed automatically when the second transaction in the conversation commits, and any modifications to the once detached and now persistent object are synchronized with the database.
Quotes from Java Persistence with Hibernate, chapter 11.2.2.