Sunday, 24 April 2016

merge vs. update


saveOrUpdate()
 does the following :

  • if the object is already persistent in this session, do nothing.
  • if another object associated with the session has the same identifier, throw an exception.
  • if the object has no identifier property, save() it.
  • if the object's identifier has the value assigned to a newly instantiated object, save() it.
  • if the object is versioned (by a or ), and the version property value is the same value assigned to a newly instantiated object, save() it otherwise update() the object.

merge() is very different : 
  • if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance.
  • if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance.
  • the persistent instance is returned.
  • the given instance does not become associated with the session, it remains detached.


So, Use : 
update()
if you are sure that the session does not contain an already persistent instance with the same identifier.

merge()
if you want to merge your modifications at any time without consideration of the state of the session.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.