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