Return to site

Difference Between Update And Saveorupdate In Hibernate

broken image


  1. Difference Between Save And Saveorupdate In Hibernate
  2. Update Vs Saveorupdate Hibernate
  3. Diff Between Save 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.

What is the difference between session.update(Object obj), session.merge(Object obj) and session.saveOrUpdate(Object obj) method of hibernate API? Session.update, session.merge and session.saveOrUpdate all three methods are used to update the records in database using hibernate persistence logic. Difference between session.save, session.saveOrUpdate and session.persist? Session.save: Save does an insert and will fail if the primary key is already persistent. Session.saveOrUpdate: saveOrUpdate does a select first to determine if it needs to do an insert or an update.Insert data if primary key not exist otherwise update data.

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. Epson l800 for mac driver. 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:

Torrent pro tools 11 for mac os. 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.

Difference Between Save And Saveorupdate In Hibernate


Output:

Free scanner for mac. 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 persist and saveorupdate in hibernate

Update Vs Saveorupdate Hibernate


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.

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.


Related Articles

Diff Between Save And Saveorupdate In Hibernate


Please enable JavaScript to view the comments powered by Disqus.comments powered by Disqus




broken image