Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Monday, March 27, 2023

Difference between save vs persist and saveOrUpdate in Hibernate

Save vs. saveOrUpdate vs. persist in Hibernate
What is the difference between save and saveOrUpdate or Difference between saving and persist are common interview question in any Hibernate interview, much like the difference between get and load method in Hibernate. Hibernate Session class provides a couple of ways to save an object into the database by methods like save, saveOrUpdate, and persist.  You can use either save(),  saveOrUpdate() or persist() based upon your requirement for persisting objects into the database. The key thing is that all these objects are used to store data into the database but they also make a transient object persistent in Hibernate.

Along with Spring framework Interview questions, Hibernate questions are also quite popular on Java interviews because of its status as leading ORM. It's good to prepare some questions from Hibernate before appearing in any J2EE interviews. One of them is the difference between save, saveOrUpdate and persist, which we will see in this Hibernate article.

And, if you are serious about improving your Hibernate and JPA skills then I also recommend you check out Master Hibernate and JPA with Spring Boot in 100 Steps course by Ranga Rao Karnam on Udemy. It's a great course to learn Hibernate and JPA with Spring Boot in a hands-on and practical manner. It's also very affordable and you can buy in just $10 on Udemy flash sales.

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. Clearly, saveOrUpdate is more flexible in terms of use but it involves extra processing to find out whether a record already exists in the table or not.

In summary, the save() method saves records into the database by INSERT SQL query, Generates a new identifier, and returns the Serializable identifier back.

On the other hand  saveOrUpdate() method either INSERT or UPDATE based upon the existence of an object in the database. If a persistence object already exists in the database then UPDATE SQL will execute, and if there is no corresponding object in the database, then INSERT will run.

If you want to learn more about these methods or Hibernate in general and need a course, I also recommend joining Java Persistence: Hibernate and JPA Fundamentals course on Udemy. It is an easy-to-understand course to learn the Java Persistence API with real-world examples.

Difference between save vs persist and saveOrUpdate in Hibernate

Difference between saving and persist method in Hibernate

Save vs SaveOrUpdate vs Persist method in HibernateIn the last section, we saw What are the difference between save and saveOrUpdate, and now we will see the difference in save vs. persist method.

1)The first difference between save and persist is there return type. Similar to save method, persist also INSERT records into the database, but return type of persist is void while return type of save is Serializable Object.  

2) Another difference between persisting and save is that both methods make a transient instance persistent. However, persist() method doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time.

This diagram also explains the life-cycle of a persistence object in Hibernate and how it moves from one state to another like Transient to Persistent to Detached. You can see that both save() and saveOrUpdate() method move an object from Transient to Persistent state. 

You can learn more about ORM and Object life cycle on Spring and Hibernate for Beginners course on Udemy, one of my favorite courses to learn both Spring and Hibernate together.

difference between save and persiste in Hibernate

3) One more thing which differentiates persist and save method in Hibernate is that it is their behavior on the outside of transaction boundaries. persist() method guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. save() method does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (like "identity" generator), this INSERT happens immediately, no matter if you are inside or outside of a transaction.

4) The fourth difference between save and persist method in Hibernate is related to previous differences in saving vs. persist. Because of its above behavior of persist method outside transaction boundary, it's useful in long-running conversations with an extended Session context. On the other hand, the save method is not good in a long-running conversation with an extended Session context.

These were some differences between save, saveOrUpdate, and persist method of Hibernate. All three methods are related to saving Objects into a database, but their behavior is quite different. Knowledge of save, persist and saveOrUpdate not only helps to decide better use of Hibernate API but also help you to do well in Hibernate interviews.

Other Hibernate Articles and Interview Questions you may like
  • The Complete Java Developer RoadMap (guide)
  • Difference between First and Second level cache in Hibernate? (answer)
  • Top 5 Courses to learn Hibernate and JPA (Courses)
  • Difference between get() and load() method in Hibernate? (answer)
  • 5 Spring and Hibernate Training Courses for Java developers (list)
  • 2 Books to Learn Hibernate from scratch (books)
  • 5 Books to Learn Spring Framework in-depth (books)
  • Why Hibernate Entity class should not be final in Java? (answer)
  • 10 Hibernate Questions from Java Interviews (list)
  • Top 5 Courses to learn Spring and Hibernate Online (courses)
  • Top 5 Courses to learn Microservices in Java (courses)
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • 5 Spring Boot Features Every Java Developer Should Learn (features)

Thanks for reading this article, if you like this article and interview question, then please share with your friends and colleagues. If you have any questions or feedback, then please drop a comment.


Unknown said...

Thanks this very helpful..........

Anonymous said...

Please elabrate the 3 & 4 points.. from the description unable to synchronize the real concept of it.

tibi said...

is persist faster than save? not needing to retrieve the created id ??

Anonymous said...

Hi Javin, could you please write a post regarding the hibernate staleobjectstateexception concurrency issue problem.

Pramod Jha said...

One more difference I would like to mention persist has to be used when cascade = cascadeType.PERSIST, If you will use save it will throw error as Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:

Nishant said...

Does save() method gets cascaded?

Post a Comment