What is difference between save and persist method in Hibernate are most important in any Hibernate interview. Hibernate Session is the interface between java application and hibernate framework. It provides some methods to move an object from new or transient state to persistent state. For example, save(), saveOrUpdate() and persist() methods are used to store an object into database. However, they have some differences about usage.
Other interesting posts you may like
Now we will create examples to clarify the difference between save and persist method in Hibernate.
Create project directory structure
Create project directory structure
In this difference between save and persist method in Hibernate tutorial, we will create the final project directory structure like below:
Create a program for testing save method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.javabycode.hibernate; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.javabycode.hibernate.model.Student; public class HibernateSaveExample { public static void main(String[] args) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Student student = new Student("David Pham", new Date(), "USA", "1234569"); Long studentId = (Long) session.save(student); System.out.println(" Identifier : " + studentId); System.out.println(" Successfully saved"); Session session2 = sessionFactory.openSession(); Transaction trans = session2.beginTransaction(); Student student2 = new Student("David Pham", new Date(), "USA", "1234569"); Long studentId2 = (Long) session2.save(student2); trans.commit(); System.out.println(" Identifier : " + studentId2); System.out.println(" Successfully saved"); sessionFactory.close(); } } |
Run above main and produce the output like below
1 2 3 4 5 6 |
Hibernate: insert into STUDENT (CODE, ENTERING_DATE, NAME, NATIONALITY) values (?, ?, ?, ?) Identifier : 34 Successfully saved Hibernate: insert into STUDENT (CODE, ENTERING_DATE, NAME, NATIONALITY) values (?, ?, ?, ?) Identifier : 35 Successfully saved |
Create a program for testing persist method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.javabycode.hibernate; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.javabycode.hibernate.model.Student; public class HibernatePersistExample { public static void main(String[] args) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Transaction trans = session.beginTransaction(); Student student = new Student("David Pham", new Date(), "USA", "1234569"); session.persist(student); trans.commit(); System.out.println(" Successfully saved"); Session session2 = sessionFactory.openSession(); Transaction trans2 = session2.beginTransaction(); Student student2 = (Student) session2.load(Student.class, new Long(33)); student2.setName("David Villa"); session2.persist(student2); trans2.commit(); System.out.println(" Successfully saved"); sessionFactory.close(); } } |
Run above main and print the output like below:
1 2 3 4 5 |
Hibernate: insert into STUDENT (CODE, ENTERING_DATE, NAME, NATIONALITY) values (?, ?, ?, ?) Successfully saved Hibernate: select student0_.id as id1_1_0_, student0_.CODE as CODE2_1_0_, student0_.ENTERING_DATE as ENTERING3_1_0_, student0_.NAME as NAME4_1_0_, student0_.NATIONALITY as NATIONAL5_1_0_, address1_.ADDRESS_ID as ADDRESS_1_0_1_, address1_.CITY as CITY2_0_1_, address1_.COUNTRY as COUNTRY3_0_1_, address1_.STREET as STREET4_0_1_ from STUDENT student0_ left outer join ADDRESS address1_ on student0_.id=address1_.ADDRESS_ID where student0_.id=? Hibernate: update STUDENT set CODE=?, ENTERING_DATE=?, NAME=?, NATIONALITY=? where id=? Successfully saved |
From the above output, the insert query is executed since the data doesn’t exist. Then we load that data by identifier and change information before calling the persist method again, here the update query is executed since the data exists.
Difference between save and persist method in Hibernate
First difference between save and persist is there return type. The return type of persist method is void while return type of save method is Serializable object. But bot of them also INSERT records into database
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.
Third difference between save and persist method in Hibernate is behavior on outside of transaction boundaries. persist() method will not execute an insert query if it is called outside of transaction boundaries. Because save() method returns an identifier so that an insert query is executed immediately to get the identifier, no matter if it are inside or outside of a transaction.
Fourth difference between save and persist method in Hibernate: persist method is called outside of transaction boundaries, it is useful in long-running conversations with an extended Session context. On the other hand save method is not good in a long-running conversation with an extended Session context.
Fifth difference between save and persist method in Hibernate: persist is supported by JPA, while save is only supported by Hibernate.
That’s it on the Difference between save saveOrUpdate and persist in Hibernate.
Reference
Hibernate Documentation
Download complete source code, please click link below
SaveAndPersist.zip (391 downloads)