SaveOrUpdate:
session.saveOrUpdate( someDetachedCat );
This is used to Reattach detached data. Reattachment is the process of taking an incoming entity instance that is in detached state and re-associating it with the current persistence context.
The method name update is a bit misleading here. It does not mean that an SQL UPDATE is immediately performed. It does, however, mean that an SQL UPDATE will be performed when the persistence context is flushed since Hibernate does not know its previous state against which to compare for changes. Unless the entity is mapped with select-before-update, in which case Hibernate will pull the current state from the database and see if an update is needed.
Provided the entity is detached, update and saveOrUpdate operate exactly the same.
Merge:
Cat theManagedInstance = session.merge( someDetachedCat );
Merging is the process of taking an incoming entity instance that is in detached state and copying its data over onto a new instance that is in managed state.
Comments
Post a Comment