Difference Between Update And Saveorupdate In Hibernate

Hibernate save vs saveOrUpdate

Posted On

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

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:

savesaveOrUpdate
Inserts a recordInserts or Updates a record
Returns the id of the inserted recordDoes not return anything, returns a void
Moves an entity from transient state to persistent stateMoves 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.

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my Hibernate course Hibernate from scratch
If you like this post, please do let me know via the comments box below. You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

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. Thankyou1
By: 0
By: 3
By: 0
By: 0
By: 0
By: 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: 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:

MOST POPULAR ARTICLES