saveOrUpdateor Difference between
persistare common interview question in any Hibernate interview, much like Difference between get and load method in Hibernate. Hibernate Session class provides couple of ways to save object into database by methods like
persist. You can use either
persist()based upon your requirement for persisting object into Database. Along with Spring framework Interview questions, Hibernate questions are also quite popular on J2EE 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 Difference between
persist, which we will see in this Hibernate article.
Difference between save and saveOrUpdate in Hibernate
Main difference between
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. Clearly
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.
Difference between save and persist method in Hibernate
In last section we saw What are difference between
saveOrUpdate and now we will see Difference on
- First difference between save and
persistis there return type. Similar to save method
persistalso INSERT records into database but return type of
persistis void while return type of save is Serializable object.
- Another difference between
persistand save is that both methods make a transient instance
persist() method doesn’t guarantee that the identifier value will be assigned to the
persistent instance immediately, the assignment might happen at flush time.
- One more thing which differentiate
persistand save method in Hibernate is that is there behaviour on 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 (e.g. ‘identity’ generator), this INSERT happens immediately, no matter if you are inside or outside of a transaction.
- Fourth difference between
persistmethod in Hibernate is related to previous difference on
persist. Because of its above behaviour of
persistmethod outside transaction boundary, its useful in long-running conversations with an extended
Sessioncontext. On the other hand save method is not good in a long-running conversation with an extended Session context.
These were some differences between
persist method of Hibernate. All three method are related to saving Object into database but there behaviour are quite different. Knowledge of
saveOrUpdate not only helps to decide better use of Hibernate API but also help you to do well in Hibernate interviews.
Don’t forget to share!
Reference: Difference between save vs persist and saveOrUpdate in Hibernate from our JCG partner Javin Paul at the Javarevisited blog.
Get ready to program in a whole new way!
Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.