What are the differences between the different saving methods in Hibernate? What are the differences between the different saving methods in Hibernate? java java

What are the differences between the different saving methods in Hibernate?


Here's my understanding of the methods. Mainly these are based on the API though as I don't use all of these in practice.

saveOrUpdateCalls either save or update depending on some checks. E.g. if no identifier exists, save is called. Otherwise update is called.

savePersists an entity. Will assign an identifier if one doesn't exist. If one does, it's essentially doing an update. Returns the generated ID of the entity.

updateAttempts to persist the entity using an existing identifier. If no identifier exists, I believe an exception is thrown.

saveOrUpdateCopyThis is deprecated and should no longer be used. Instead there is...

mergeNow this is where my knowledge starts to falter. The important thing here is the difference between transient, detached and persistent entities. For more info on the object states, take a look here. With save & update, you are dealing with persistent objects. They are linked to a Session so Hibernate knows what has changed. But when you have a transient object, there is no session involved. In these cases you need to use merge for updates and persist for saving.

persistAs mentioned above, this is used on transient objects. It does not return the generated ID.


╔══════════════╦═══════════════════════════════╦════════════════════════════════╗║    METHOD    ║            TRANSIENT          ║            DETACHED            ║╠══════════════╬═══════════════════════════════╬════════════════════════════════╣║              ║       sets id if doesn't      ║   sets new id even if object   ║║    save()    ║     exist, persists to db,    ║    already has it, persists    ║║              ║    returns attached object    ║ to DB, returns attached object ║╠══════════════╬═══════════════════════════════╬════════════════════════════════╣║              ║       sets id on object       ║             throws             ║║   persist()  ║     persists object to DB     ║       PersistenceException     ║║              ║                               ║                                ║╠══════════════╬═══════════════════════════════╬════════════════════════════════╣║              ║                               ║                                ║║   update()   ║           Exception           ║     persists and reattaches    ║║              ║                               ║                                ║╠══════════════╬═══════════════════════════════╬════════════════════════════════╣║              ║  copy the state of object in  ║    copy the state of obj in    ║║    merge()   ║     DB, doesn't attach it,    ║      DB, doesn't attach it,    ║║              ║    returns attached object    ║     returns attached object    ║╠══════════════╬═══════════════════════════════╬════════════════════════════════╣║              ║                               ║                                ║║saveOrUpdate()║           as save()           ║            as update()         ║║              ║                               ║                                ║╚══════════════╩═══════════════════════════════╩════════════════════════════════╝


  • See the Hibernate Forum for a explanation of the subtle differences between persist and save. It looks like the difference is the time the INSERT statement is ultimately executed. Since save does return the identifier, the INSERT statement has to be executed instantly regardless of the state of the transaction (which generally is a bad thing). Persist won't execute any statements outside of the currently running transaction just to assign the identifier.Save/Persist both work on transient instances, ie instances which have no identifier assigned yet and as such are not saved in the DB.

  • Update and Merge both work on detached instances, ie instances which have a corresponding entry in the DB but which are currently not attached to (or managed by) a Session. The difference between them are what happens to the instance which is passed to the function. update tries to reattach the instance, that means that there must be no other instance of the persistent entity attached to the Session right now, otherwise an exception is thrown. merge, however, just copies all values to a persistent instance in the Session (which will be loaded if it is not currently loaded). The input object is not changed. So merge is more general than update, but may use more resources.