0

I have got below piece of code which shows difference between update and merge method of hibernate. But still I do not get why do we need line no#13 here. If we do not write line#13 is it possible to call session2.update(s1) at place of line no#17

SessionFactory factory = cfg.buildSessionFactory();  //line no#1
Session session1 = factory.openSession();            //line no#2
                                                     //line no#3
Student s1 = null;                                   //line no#4
Object o = session1.get(Student.class, new Integer(101)); //line no#5
s1 = (Student)o;                                     //line no#6
session1.close();                                   //line no#7
                                                    //line no#8
s1.setMarks(97);                                    //line no#9
                                                    //line no#10
Session session2 = factory.openSession();           //line no#11
Student s2 = null;                                  //line no#12
Object o1 = session2.get(Student.class, new Integer(101)); //line no#13
s2 = (Student)o1;                                   //line no#14
Transaction tx=session2.beginTransaction();         //line no#15
                                                    //line no#16
session2.merge(s1);                                 //line no#17
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
saurabh gupta
  • 57
  • 1
  • 9
  • 1
    **Hibernate update** should be used where we know that we are only updating the entity information. This operation adds the entity object to persistent context and further changes are tracked and saved when transaction is committed. **Hibernate merge** can be used to update existing values, however this method create a copy from the passed entity object and return it. The returned object is part of persistent context and tracked for any changes, passed object is not tracked. This is the major difference with merge() from all other methods. – IMParasharG Dec 30 '18 at 10:40
  • With reference to code snippet, My understanding says, even if we remove line no#13 and line no#14, this will work. Second case is If we remove line no#13 & line no#14 and write update method instead of merge then also it will work. correct me please if i am wrong? – saurabh gupta Dec 30 '18 at 16:36

1 Answers1

0

To understand the difference you first need to understand Persistent Objects in hibernate which is explained in following answer - What does persistence object means in Hibernate architecture?

In simple terms, Persistent objects are instances of POJO classes that you create that represent rows in the table in the database. Now coming back to merge and update -

update method Updates the persistent instance with the identifier of the given detached instance. If there is a persistent instance with the same identifier, an exception is thrown.

while merge method Copies the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance.

Deepak Tripathi
  • 600
  • 2
  • 4
  • 22
  • I understood the difference between update and merge, still need more clarity. In the code snippet I have given got from some article. I am not getting why line no#13 he wrote. My understanding says, even if we remove line no#13 and line no#14, this will work. Second case is If we remove line no#13 & line no#14 and write update method instead of merge then also it will work. correct? – saurabh gupta Dec 30 '18 at 16:34