March 26, 2023

Blog @ Munaf Sheikh

Latest news from tech-feeds around the world.

Difference between save(), saveOrUpdate() and persist() in Hibernate Session

The Session interface in Hibernate provides a couple of methods to move an object from a new or transient state to a persistent state like save(), saveOrUpdate(), and persist() is used to store an object into the database, but there are some significant differences between them. The method does an INSERT to store the object into the database and it also returns the identifier generated by the database. On the other hand, saveOrUpdate() can be used to reattach a detached object in Hibernate Session i.e. it can do INSERT or UPDATE depending upon whether an object exists in the database or not. 

It is also one of the frequently asked Hibernate interview questions on Java Web developer job interviews. Knowing this detail helps you to do well in your interview. vs Session.saveOrUpdate() vs Session.Persist

As I said, all three methods save(), persist() and saveOrUpdate belongs to Hibernate’s Session interface. They all are used to bring a new object into the persistence context or a detached object, which was associated with a Hibernate Session in past but currently it’s not associated with the active persistence context.

It is also one of the most popular Hibernate interview questions. Let’s see couple of more differences between save(), persist() and saveOrUpdate() methods of Session interface.

1. The main difference between the save() and saveOrUpdate() method is that the save() method performs an INSERT operation to store the object into the database, but INSERT 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() with the detached object will fail. 

This is the opposite of the saveOrUpdate() method, which can do either INSERT or UPDATE SQL query depending upon whether an object exists in the database or not. The saveOrUpdate() method first executes a SELECT query to determine if it needs to do an INSERT or UPDATE operation.

2. Another key difference between the save() and saveOrUpdate() method is that the former is used to bring a transient object to a persistent state but saveOurUpdate() can bring both transient (new) and detached (existing) objects into the persistent state. It is often used to re-attach a detached object into a Session.

Here is an object’s lifecycle and state transition diagram in Hibernate which shows that save() and saveOrUpdate() can move an object from transient to persistent state and if you are new to Hibernate, you can also see Master Hibernate and JPA with Spring Boot in 100 Steps course on Udemy to learn Hibernate from scratch. 

3. Coming to the persist() method, the difference between the save() and persist() method is that the former returns the generated database identifier, a Serializable object but the persist() method doesn’t return anything. Its return type is void.

For example:


will print the generated primary key, but the following line will throw a compile-time error because persist()‘s return type is void.


That’s all about difference between save(), saveOrUpdate() and persist() method of Hibernate Session interface. Now you know the basic difference between them and also learned when to use the save(), saveOrUpdate(), and persist() method. 

Use the save() method to store new objects into the database and when you need the generated database identifier, otherwise use the persist() method. You can use saveOrUpdate() to reattach a detached object into Hibernate Session.

Other Hibernate Articles and Interview Questions you may like

  • 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)
  • The Complete Java Developer RoadMap (guide)
  • 2 Books to Learn Hibernate from scratch (books)
  • 5 Spring and Hibernate Training Courses for Java developers (list)
  • 5 Books to Learn Spring Framework in-depth (books)
  • Top 5 Courses to learn Spring and Hibernate Online (courses)
  • Why Hibernate Entity class should not be final in Java? (answer)
  • 10 Free Courses to learn Spring Framework (Free courses)
  • 10 Hibernate Questions from Java Interviews (list)
  • 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 the interview question then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment.


Source link