Register Login

Difference between Save and Persist in Hibernate with Comparison Chart

Updated Mar 25, 2019

The common interview questions related to save in hibernate and save in hibernate and persist in hibernate have to be understood in depth to give an accurate reply. The difference between persist and save in Hibernate sessions will help you understand easy modes of saving object into databases.

Save() or persist() can be utilized on the basis of the need for persisting objects into a specific database. Through this article, we aim to help you understand when to use persist in hibernate in Spring framework projects. Given below is a hibernate save vs persist comparison to trigger off the paras that follow.

Hibernate Save vs Persist

Basis of Difference

Save in Hibernate

Persist in Hibernate

Return type

The return type pertaining to the save ()
is serializable object.

The return type of persist () method is void.

Assigning of identifier value

Th save() method allows for the assigning of identifier value instantly.

The persist() method fails to guarantee that an identifier value is assigned to its persistent state instantly.

Execution of insert query

The save() method provides an identifier with the intent of an insert query being executed immediately for getting the identifier. It does not matter whether it is outside or inside a transaction.

The persist() method fails to execute a given insert query in case it is placed outside transaction boundaries.

Utility

The save method proves to be of less use in a long-running conversation that has extended a given Session context.

As the persist method is called outside the transaction boundaries, it is utilized in long-running conversations that offer an extended Session context.

Support

Save() method gets support only through Hibernate.

Persist() method is aptly supported by JPA.

Hibernate Persist

Hibernate persist has a striking similarity with save session in hibernate (with transaction). It is responsible for adding the entity object to its persistent context. It does so with a view of enabling the tracking of any further changes. In case the properties of an object are changed prior to the transaction being performed, or the session being flushed, then the same will be saved into a database.

Hibernate Save

Hibernate save() is useful for saving an entity to a given database. This method can be invoked outside of a given transaction. It is a commonly used method of saving data. In case this method is used without any transaction, and if there exists cascading between entities, only then will the primary entity be saved. The session can be flushed to get the same result.

Conclusion

The above features, definition and comparison of Save() and persist() methods will help you figure out the application of both. In case you have any further queries with respect to save in hibernate and persist in hibernate then do write back to us, we will be glad to provide further inputs instantly.


×