In this Video, You will learn about save persist and saveorupdate methods in hibernateBelow is the GitHub link to download source:https://github.com/kishanja. Difference between update and saveOrUpdate In hibernate update method will perform updation on a record in database table if the specified record is existed otherwise it. The Session interface in Hibernate provides a couple of methods to move an object from a new or transient state to a persistent state e.g. Save, saveOrUpdate, and persist are used to store an object into the database, but there are some significant differences between them. The Session.save method does an INSERT to store the object into the database and it also returns.
- Diff Between Save And Saveorupdate In Hibernate
- Difference Between Save Persist And Saveorupdate In Hibernate
What is difference between save and saveOrUpdate or Difference between save and persist are most important question in any Hibernate interview.
Session interface in Hibernate provides a couple of methods to move an object from new or transient state to persistent state e.g. save(), saveOrUpdate() and persist() are used to store an object into the database, but there are some significant differences between them.
Difference between save() and saveOrUpdate() in Hibernate
- Main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into database while saveOrUpdate can either INSERT or UPDATE based upon existence of record.save() method will fail if the primary key is already persistent i.e. object already exists in the database. This is why, you should only call save() with an absolutely new object which doesn't have any database identifier. Calling save() method on the detached object will throw exception.
- Hibernate Basics
- Hibernate Inheritance Mapping
- Hibernate Query Language (HQL)
What is difference between save and saveOrUpdate or Difference between save and persist are most important question in any Hibernate interview.
Session interface in Hibernate provides a couple of methods to move an object from new or transient state to persistent state e.g. save(), saveOrUpdate() and persist() are used to store an object into the database, but there are some significant differences between them.
Difference between save() and saveOrUpdate() in Hibernate
- Main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into database while saveOrUpdate can either INSERT or UPDATE based upon existence of record.save() method will fail if the primary key is already persistent i.e. object already exists in the database. This is why, you should only call save() with an absolutely new object which doesn't have any database identifier. Calling save() method on the detached object will throw exception.
- Hibernate Basics
- Hibernate Inheritance Mapping
- Hibernate Query Language (HQL)
- The Session.save() method does an INSERT to store the object into the database and return 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. saveOrUpdate is more flexible in terms of use but it involves an extra processing to find out whether record already exists in table or not.saveOrUpdate does a select first to determine if it needs to do an insert or an update. It will insert data if primary key not exist otherwise it will update data.
- Another key difference between save() and saveOrUpdate() method is that save() method is used to bring a transient object to persistent state but saveOurUpdate() can bring both transient (new) and detached (existing) object into persistent state. It is often used to re-attach a detached object into Session.
Summary:
save() method saves records into database by INSERT SQL query, Generates a new identifier and return the Serializable identifier back. On the other hand saveOrUpdate() method either INSERT or UPDATE based upon existence of object in database. If persistence object already exists in database then UPDATE SQL will execute and if there is no corresponding object in database than INSERT will run.
Example:
Below is example of both save and saveOrUpdate() method. You can easily identify the difference in generated SQL statement in the console.
Entity Class
Configuration File
save() method
Output:
Look at the above output carefully, save() method first generates identifier and then inserts data into database. We printed the value of the identifier after saving the object. You can clearly see the insert query in the console output.
saveOrUpdate() method
Output:
The above output shows that the entity with specified identifier does not exist in database by running select query. So it applied insert query to persist data.
Now check the output for such situation where entity for the given identifier is already exist in database. In the above example just change the name and phone number. Modified code is given below.
Output:
In the above output, first select query runs to check existence of data for given identifier and since data is there, so update query runs to update data.
Difference between save() and persist() methods
- Main difference of save() and persist() is return type of the save() method is java.io.Serializable it returns the generated identity value whereas the return type of persist method is void i.e, it will not return any value.persist() is similar to Session.save() i.e. it is used to move a transient object to the persistent state by storing it into the database but it doesn't return the database identifier.
- Another key difference is Persist method can be used only within the boundary of a transaction whereas save method can be used inside or outside the transaction boundaries.persist() method guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. But in the case of save() INSERT happens immediately, no matter if you are inside or outside of a transaction. This is not good in a long-running conversation with an extended Session/persistence context.
Diff Between Save And Saveorupdate In Hibernate
For example:
Above code will print the generated primary key because save() method will return the generated identifier, but the below line of code will throw a compile time error because persist() method return type is void.