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.
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.
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.
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.
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.
More Stories like this
Understanding “Lifting State Up” in React – Example Tutorial
Higher Order Components in React – Example Tutorial
5 Best PowerPoint Courses for IT Professionals in 2022