What is difference between save and saveOrUpdate 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 saveOrUpdate in Hibernate.
Create project directory structure
In this Difference between save and saveOrUpdate 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 saveOrUpdate 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 HibernateSaveOrUpdateExample { 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.saveOrUpdate(student); trans.commit(); System.out.println(" Successfully saved"); Session session2 = sessionFactory.openSession(); Transaction trans2 = session2.beginTransaction(); Student student2 = (Student) session.load(Student.class, new Long(32)); student2.setName("David Villa"); session2.saveOrUpdate(student2); trans2.commit(); System.out.println(" Successfully saved"); sessionFactory.close(); } } |
Run above main and print the output like this:
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 |
In 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 saveOrUpdate method again, here the update query is executed since the data exists.
Difference between save and saveOrUpdate in Hibernate
Main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into database while saveOrUpdate can either INSERT or UPDATE based upon existence of record. If object already exists in the database, save() method will fail.
The save() method returns the identifier generated by the database. On the other hand, saveOrUpdate() can do INSERT or UPDATE depending upon whether object exists in database or not. saveOrUpdate does a select first to determine if it needs to do an insert or an update.
Another key difference between save() and saveOrUpdate() method is that save() method is used to make a transient object to persistent state but saveOurUpdate() can make both transient (new object) and detached (existing object) object into persistent state. So that saveOrUpdate() is often used to re-attach a detached object into Session.
That’s it on the difference between save and saveOrUpdate in Hibernate tutorial.
Download complete source code, please click link below
SaveAndSaveOrUpdate.zip (286 downloads)