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 Session.save() 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. 

The third method, persist() is similar to the Session.save(), I mean, 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. This was the basic difference between the save() vs saveOrUpdate() vs persist() method and suggest when to use which method. 

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. 

Btw, if you need a resource to learn Spring and Hibernate in-depth then you can join Spring and Hibernate for Beginners course on Udemy the best online course to learn these kinds of subtle details about Hibernate.




Session.save() 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:
System.out.println(session.save(aCoin));

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

System.out.println(session.persist(aCoin));


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.
 

5 comments:

  1. What does saveOrUpdate(obj) return?

    ReplyDelete
  2. There is no return type
    it's a void method
    Look to the signature
    void saveOrUpdate(Object object)
    void saveOrUpdate(String entityName, Object object)

    ReplyDelete
  3. "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. "
    Are you sure about it?

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.