Difference between save and persist method in Hibernate

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.

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

packagecom.javabycode.hibernate;

import java.util.Date;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import com.javabycode.hibernate.model.Student;

publicclassHibernateSaveExample{

publicstaticvoidmain(String[]args){

SessionFactory sessionFactory=HibernateUtil.getSessionFactory();

Session session=sessionFactory.openSession();

Student student=newStudent("David Pham",newDate(),"USA","1234569");

LongstudentId=(Long)session.save(student);

System.out.println(" Identifier : "+studentId);

System.out.println(" Successfully saved");

Session session2=sessionFactory.openSession();

Transaction trans=session2.beginTransaction();

Student student2=newStudent("David Pham",newDate(),"USA","1234569");

LongstudentId2=(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

packagecom.javabycode.hibernate;

import java.util.Date;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import com.javabycode.hibernate.model.Student;

publicclassHibernatePersistExample{

publicstaticvoidmain(String[]args){

SessionFactory sessionFactory=HibernateUtil.getSessionFactory();

Session session=sessionFactory.openSession();

Transaction trans=session.beginTransaction();

Student student=newStudent("David Pham",newDate(),"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,newLong(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(?,?,?,?)

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.