Difference between save() and persist() in Hibernate


Save() and persist() both methods are used for saving object in the database. 

As per docs −

Save()  − Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.) This operation cascades to associated instances if the association is mapped with cascade="save-update".

As per docs −

persist() −  Make a transient instance persistent. This operation cascades to associated instances if the association is mapped with cascade="persist". The semantics of this method are defined by JSR-220. 

Sr. No.
Key
save()
persist()

1

Basic 

 It stores object in database

It also stores object in database

2

Return Type 

It return generated id and return type is serializable 

It does not return anything. Its void return type. 

3

Transaction Boundaries 

It can save object within boundaries and outside boundaries 

It can only save object within the transaction boundaries 

4.

Detached Object 

It will create a new row in the table for detached object  

It will throw persistence exception for detached object 

5.

Supported by 

It is only supported by Hibernate 

It is also supported by JPA

Example of Save in Hibernate

@Entity
public class User {
   @Id
   Integer id;
   String name;
   public Integer getId() {
      return id;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.journaldev.hibernate.util.HibernateUtil;
public class SaveExample {
   public static void main(String[] args) {
      //get session factory to start transcation
      SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
      Session session = sessionFactory.openSession();
      Transaction tx = session.beginTransaction();
      User user = new User();
      user.setId(1)
      user.setName("ABCD")
      session.save(user);
      //Close resources
      tx.commit();
      sessionFactory.close();
   }
}

Example of Persist in Hibernate

@Entity
public class User {
   @Id
   Integer id;
   String name;
   public Integer getId() {
      return id;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.journaldev.hibernate.util.HibernateUtil;
public class PersistExample {
   public static void main(String[] args) {
      //get session factory to start transcation
      SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
      Session session = sessionFactory.openSession();
      Transaction tx = session.beginTransaction();
      User user = new User();
      user.setId(1)
      user.setName("ABCD")
      session.persist(user);
      //Close resources
      tx.commit();
      sessionFactory.close();
   }
}

Updated on: 21-Jan-2020

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements