See this answer, saveOrUpdate
works well if there is never the risk that there is not already an object with the same database identifier associated to the session where saveOrUpdate
is being called.
If an object already exists with the same database Id, saveOrUpdate throws an error, while merge will just replace the object in the session with the new object and return a reference to the new attached object.
Hibernate had a method called saveOrUpdateCopy that provided the same as current merge
,
which got standardized as merge in JPA.
Although saveOrUpdate
is still available and used in many tutorials, you would be better off always using merge
when dealing with detached objects, as it's hard / error prone to try to guess if an object is already in a session.