Difference between save vs persist methods in Hibernate

Whenever we are using ORM like Hibernate, there are two from many others methods save() and persist() using to save object to database. But these two methods have the differences in functionality. And also difference between hibernate save() and persist() methods is depends on generator class we are using. There are following few differences as below.

1. Suppose we are using generator class to assign the primary key to persisting object, then there is no difference between these two methods because in generate class programmer we need to give the primary key value to save in the database.

2. persist() is supported by JPA, while save() is only supported by Hibernate.
Popular Spring Tutorials

  1. Spring Tutorial
  2. Spring MVC Web Tutorial
  3. Spring Boot Tutorial
  4. Spring JDBC Tutorial
  5. Spring AOP Tutorial
  6. Spring Security Tutorial

3. Main difference between save and persist is there return type. save () method return Serializable object but persist() method return void.

Serializable pk = session.save(object);

4. Another difference between save and persist method in Hibernate is behavior on outside of transaction boundaries. There is no boundary of transaction for save() method because it returns an identifier so that an insert query is executed immediately to get the identifier. But persist() method will not execute an insert query if it is called outside of transaction boundaries.

5. Another difference between persist and save is that both methods make a transient object to persistent state. However, persist() method doesn’t guarantee that the identifier value will be assigned to the persistent state immediately, the assignment might happen at flush time.

Previous
Next