Difference Between Update And Saveorupdate In Hibernate
Hibernate save vs saveOrUpdate
Hibernate provides several methods to save or modify the data into a database table. Two such methods are save and saveOrUpdate. In this blog post, I will be providing a detail comparison between the two.
What is save?
Hibernate - difference between Session.save and Session.saveOrUpdate method The main difference between Session.save and Session.saveOrUpdate method is that save generates a new identifier and INSERT record into a database while Session.saveOrUpdate can either INSERT or UPDATE based upon existence of a record. So saveOrUpdate method calls save method if there is no record in database, and it calls update method if there is a record in database. Related Posts: getTransaction,beginTransaction,getIdentifier in Hibernate; CRUD operations using Hibernate+ Maven+ Oracle+ XML mapping.
![Difference Between Update And Saveorupdate In Hibernate Difference Between Update And Saveorupdate In Hibernate](https://i.stack.imgur.com/rJY2u.png)
Hibernate provides the save method on the session interface to save a record into the database. So the session.saveinserts a record into the database. It returns the id, that is the primary key that will be assigned to the database record.
What is saveOrUpdate?
In addition to the save method, Hibernate also provides a method called saveOrUpdate on the session interface. Just as the name suggests, this method either performs a save operation or an update operation. If there is no record in the database corresponding to the object passed in, it inserts a record, but if there is a record corresponding to the object passed in, it just updates the existing record. This is generally useful in applications where a UI is involved. In such cases the back end might not be sure if the UI has sent a new object or an existing object with some updated fields. So instead of writing explicit code in the back end to check if a record exists, the saveOrUpdate method can be used.
How are they similar?
Both the save and the saveOrUpdate methods can be used to insert a record to the database. Also, both methods are Hibernate’s proprietary methods and are not part of the JPA specification.
How are they different?
Both the save and the saveOrUpdate methods can be used to insert a record to the database, there are a couple of differences between the two. The save method just inserts a record into the database. So when the code invokes the save method , Hibernate translates it to an SQL Insert query. In contrast, the saveOrUpdate method can either save or update a record in the database. So when the code invokes the saveOrUpdate method, Hibernate translates it into either an SQL Insert query or an SQL Update query. Another difference is that the save method returns the id of the newly inserted record. The saveOrUpdate method does not return anything, it returns a void. Another difference is that the save method operates on a transient object, i.e. it moves an entity from the transient state to the persistent state. In contrast, the saveOrUpdate method moves an object from the transient or detached state to the persistent state. I will be covering the entity states in a separate post. The following table summarizes the differences:
save | saveOrUpdate |
---|---|
Inserts a record | Inserts or Updates a record |
Returns the id of the inserted record | Does not return anything, returns a void |
Moves an entity from transient state to persistent state | Moves an entity from transient or detached state to persistent state |
Which should you use and when?
Difference Between Merge And Saveorupdate In Hibernate
You should generally use the save method when you are sure that there is no record in the database corresponding to the object passed in. So for example, suppose the back end receives an object from the UI. Suppose, there is a new user registration screen in the UI. When the object is sent by this screen to the back end, you are sure that there will be no record in the database corresponding to the object passed in. In such a case, you can use the save method. On the other hand, you should use the saveOrUpdate if you are not sure if the UI has sent a new object or an existing object with some updated fields.
Related posts:
Update will update the record. Merge also do the same but difference is update raise the error if use record not availale but merge can't instead it will create the record. Check it once. Thankyou | 1 |
By: Camkcdc@yahoo.com On: Tue Mar 19 16:30:19 IST 2013 21150 | |
Are You Satisfied :14Yes25No |
Merge: merge is like combining records from more than one table(while retreving records from tables based on some conditions) Update: Update is like edit . use to change the value of record.. Update():- if you are sure that the session does not contains an already persistent instance with the same identifier,then use update to save the data in hibernate Merge():-if you want to save your modificatiions at any time with out knowing abot the state of an session, then use merge() in hibernate. | 0 |
By: malikravi908@gmail.com On: Thu Mar 21 13:03:37 IST 2013 03920 | |
Are You Satisfied :21Yes22No |
Merge: suppose we create a session and load an object. Now object in session cache. If we close the session at this point and we edit state of object and tried to save using update() it will throw exception. To make object persistent we need to open another session. Now we load same object again in current session. So if we want to update present object with previous object changes we have to use merge() method. Merge method will merge changes of both states of object and will save in database. Update: If we are dealing with any object in same session we should use update() or saveOrUpdate() method. Hope you will get point. | 3 |
By: kdmalviyan@gmail.com On: Wed Jan 15 18:44:23 IST 2014 030 | |
Are You Satisfied :18Yes8No |
Great explanations... | 0 |
By: charansingh24@gmail.com On: Tue Dec 29 22:12:22 IST 2015 000 | |
Are You Satisfied :0Yes0No |
nice explanation... | 0 |
By: charansingh24@gmail.com On: Tue Dec 29 22:13:35 IST 2015 000 | |
Are You Satisfied :0Yes0No |
If session does not contains an already persistent instance with the same identifier and if you are sure about that then use update to save the data. But merge() method can save your modifications at any time with out having the knowledge about the state of session. | 0 |
By: abhishekitmits@gmail.com On: Sat Aug 27 17:01:28 IST 2016 000 | |
Are You Satisfied :0Yes0No |
Difference Between Save Persist And Saveorupdate In Hibernate
Hi All, Greetings for the day! merge() and update() both are used to convert state of an object from detached to persistent but the difference is that if update sees that the object is already present in the session cache then it will throw an exception (NonUniqueObjectException) whereas merge() will not throw any such exception. Example --- Session session = sessionFactory.openSession(); Student me = session.get(Student.class, new Integer(101)); session.close(); me.setName('updated Debayan'); Session newSession = sessionFactory.openSession(); Student updatedMe = session.get(Student.class, new Integer(101)); Transaction tx = newSession.beginTransaction(); newSession.update(me); // will throw exception newSession.merge(me); // will run fine Explanation -- Here we closed the first session and Student object me became detached. After that we changed the name property of that detached object. Now if we call update() we will get exception because before reattachment, another instance that represents the same database row has already been loaded into the persistence context of that Session and Hibernate will get confused which object represents the current state. However, for merge() into updatedMe object, changes of me object will be merged and will finally be saved into the database. Hope this clarifies the doubts ! Have a great day ahead !! | 0 |
By: debayanbasu11@gmail.com On: Sun Jun 25 17:54:07 IST 2017 000 | |
Are You Satisfied :0Yes0No |
Difference Between Update And Saveorupdate In Hibernate Windows 10
Hi All, Greetings for the day! merge() and update() both are used to convert state of an object from detached to persistent but the difference is that if update sees that the object is already present in the session cache then it will throw an exception (NonUniqueObjectException) whereas merge() will not throw any such exception. Example --- Session session = sessionFactory.openSession(); Student me = session.get(Student.class, new Integer(101)); session.close(); me.setName('updated Debayan'); Session newSession = sessionFactory.openSession(); Student updatedMe = session.get(Student.class, new Integer(101)); Transaction tx = newSession.beginTransaction(); newSession.update(me); // will throw exception newSession.merge(me); // will run fine Explanation -- Here we closed the first session and Student object me became detached. After that we changed the name property of that detached object. Now if we call update() we will get exception because before reattachment, another instance that represents the same database row has already been loaded into the persistence context of that Session and Hibernate will get confused which object represents the current state. However, for merge() into updatedMe object, changes of me object will be merged and will finally be saved into the database. Hope this clarifies the doubts ! Have a great day ahead !! | 0 |
By: debayanbasu11@gmail.com On: Sun Jun 25 18:02:31 IST 2017 000 | |
Are You Satisfied :0Yes0No |