Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences among save, update, saveOrUpdate, merge methods in Session?

Tags:

hibernate

I am new to Hibernate and went through the Hibernate tutorial last week. I have a few doubts about methods save, update, saveOrUpdate and merge in the Session class. These are:

  • save method: it is used to insert the newly created object in the datastore. (Basically identifier value will be 0 for this). Like I create a new customer and call save operation, it will persist it in the datastore and generate the identifier.

    Is this correct? And if we call save on already persistent object not sure what will happen?

  • update method: it is used to update the already persistent object in the datastore.(Basically identifier value will be some non zero value for this). Like I load a new customer and call update operation after update of some field value, it will update it in datastore.

    As per my understanding it should fail with some exception because as per API update is for detached object. Is this correct? If yes, what should we call to update the object in the same session (I mean if object is not detached). Another point is: what will happen if we call update on newly created object?

  • saveOrUpdate method: it will call either of above based on unsaved-value checks (which it must be doing based on identifier zero or non zero value, right?) so if we have persistent customer object and we update last name of his and create a new account also, then saveOrUpdate will take care of it.

    Did I understand that correctly?

  • Merge method: it will act like update but here if a persistent object with the same identifier is already in the session it will update the detached object values in the persistent object and save it.

    But if there is no persistent instance currently associated with the session, this will load the persistent object from the datastore and then update the value of detached object in loaded persistent object and then update it.

    Did I also get that?

like image 825
M Sach Avatar asked Sep 19 '11 18:09

M Sach


People also ask

What is the difference between Save () merge () saveOrUpdate ()?

You should apply the differnce between save() and saveOrUpdate method in your code to get the best performance: The save() method returns the identifier generated by the database. On the other hand, saveOrUpdate() can do INSERT or UPDATE depending upon whether object exists in database or not.

What is the difference between update and merge method?

A merge() method is used to update the database. It will also update the database if the object already exists. An update() method only saves the data in the database. If the object already exists, no update is performed.

What is the difference between session Save () and session saveOrUpdate () methods in Hibernate?

Difference between save and saveOrUpdate in Hibernate The main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record.

What is the difference between save and merge in Hibernate?

Once save/update is done, the object DOES NOT reflect the change. The returned object reflects the changes, and it is attached to hibernate session. MERGE method offers greater flexibility when it comes to saving data objects, since you need not worry about attaching object to Session.


2 Answers

You have most things right, but update works a little differently from how you were describing it. If an object is in the session (i.e. persistant), update is completely unnecessary. Hibernate handles persisting any changes to objects in the session when the session is flushed. Calling update on them will do nothing (and may incur a performance penalty; I'm not sure).

Update is designed to be called on detached objects, i.e. those that are now outside the session they were loaded in. @hvgotcodes explanation seems to be incorrect, because update should only be called if the object is not in the session. update can fail if an instance of the object is already in the session. Merge should be used in that case. It merges the changes of the detached object with an object in the session, if it exists. If there's no object in the session, it will create a new one.

So often you can avoid calling update/merge at all, but if you end up having to call one, merge handles a broader range of situations. My understanding is the only reason to use update is for better performance, assuming you know it won't error.

This thread has a pretty good summary of some other hibernate methods, as well.

Edit: I just thought I should say there are more differences between merge and update than I originally said. Update modifies the given entity to be persistent, whereas merge returns a new persistent entity. For merge, you're supposed to throw away the old object. Another difference is merge does a dirty check by selecting from the DB before deciding whether to write its data, whereas update always persists its data to the DB whether it's dirty or not.

There are probably other small differences. It's always good to test Hibernate behavior out by logging the generated SQL, because the behavior doesn't always match the docs, at least in my experience.

like image 120
Naliba Avatar answered Sep 22 '22 02:09

Naliba


You are exactly right in all of your assessments. You get it.

For your first question, if i recall correctly, save specifically does an insert. So calling save again will result in another row in the db.

For your second question, update updates an object in the session. So if the object is in the session it will update. If the object is not in the session, you should call merge. I believe calling update for a detached instance will result in an exception.

like image 41
hvgotcodes Avatar answered Sep 25 '22 02:09

hvgotcodes