Sunday 15 July 2018

Difference between save() and persist() method in Hibernate

           In previous post, we learned the difference between get() and load() methods in Hibernate. In this, we can discuss difference between the save() and persist() methods of Hibernate. This is one of the famous Hibernate interview question.

Difference between save() and persist() methods are as follows,

save and persist method in hibernate


save() method example:--


        The save() method which takes a transient object as input and put it to its persistent state. This method returns a generated identifier. 

package com.adnblog;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.adnblog.Employee;

public class HibernateSaveExample {

       public static void main(String[] args) {

             SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
             Session session = sessionFactory.openSession();
             Transaction txn = session.beginTransaction(); 

             Employee emp = new Employee();
             emp.setName("Mahesh");
             emp.setAddress("Sonyal");
  
             int id = (Integer) session.save(emp);   //save is called which will add this transient object to persistent state.
  
             txn.commit();                     
             sessionFactory.close();
      }
}


persist() method example:--


            In the same way persist() is another method to store objects in database i.e. changing transient objects to its persistent state. But persist() method does not return anything.

package com.adnblog;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.adnblog.Employee;

public class HibernatePersistentExample {

      public static void main(String[] args) {

            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            Session session = sessionFactory.openSession();
            Transaction txn = session.beginTransaction();

            Employee emp = new Employee();
            emp.setName("Mahesh");
            emp.setAddress("Sonyal");
  
            session.persist(emp);          //persist is called which will add this transient object to persistent state.
  
            tx.commit();                     
            sessionFactory.close();
      }
}

Thanks for visiting blog.....


Related Posts:--
1) What is the difference between get() and load() methods in Hibernate?
2) Hibernate Criteria Queries and Examples
3) Hibernate Query Language(HQL) Examples
4) What is a Hibernate Caching ? Explain first level and second level cache in Hibernate
5) What are different states of an entity bean in Hibernate?
6) Hibernate One to One Mapping Example - Annotation based
7) Hibernate - JPA Annotations with explanation
8) Advantages of Hibernate over JDBC

1 comment:

  1. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. This paragraph gives clear idea for the new viewers of blogging, Thanks you. You’re doing a great job Man, Keep it up.
    JAVA Interview Questions and Answers

    ReplyDelete