Difference Between Merge And Update Methods In Hibernate
|
Hibernate »
On Sep 12, 2011 | { 22 Comments }
|
Tweet
|
Both update() and merge() methods in hibernate are used to convert the object which is in detached state into persistence state. But there is little difference. Let us see which method will be used in what situation.
Let Us Take An Example
------ ----- SessionFactory factory = cfg.buildSessionFactory(); Session session1 = factory.openSession(); Student s1 = null; Object o = session1.get(Student.class, new Integer(101)); s1 = (Student)o; session1.close(); s1.setMarks(97); Session session2 = factory.openSession(); Student s2 = null; Object o1 = session2.get(Student.class, new Integer(101)); s2 = (Student)o1; Transaction tx=session2.beginTransaction(); session2.merge(s1);
Explanation
- See from line numbers 6 – 9, we just loaded one object s1 into session1 cache and closed session1 at line number 9, so now object s1 in the session1 cache will be destroyed as session1 cache will expires when ever we say session1.close()
- Now s1 object will be in some RAM location, not in the session1 cache
- Hear s1 is in detached state, and at line number 11 we modified that detached object s1, now if we call update() method then hibernate will throws an error, because we can update the object in the session only
- So we opened another session [session2] at line number 13, and again loaded the same student object from the database, but with name s2
- so in this session2, we called session2.merge(s1); now into s2 object s1 changes will be merged and saved into the database
Hope you are clear…, actually update and merge methods will come into picture when ever we loaded the same object again and again into the database, like above.
|
What you are thinkig....
22 Responses to “Difference Between Merge And Update Methods In Hibernate”
If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum




Nice explanation i m surprise there is no comment for this this explanation cleat most of my doubt Thanx
@Sanket
You bet
, thanks for your feedback on this.
Your’s explanation is like flow of river in calm state.I am glad to see your site hear from your code.
@Jaffar
Thanks much
Glad to hear your feedback.
Its fabulous and great way of explaining things, I just love it.
I was always confused with these two methods but your way of explanation is simply awesome. Now I will never forget the difference. Thanks !!
@Rajesh,@Harish
I am really happy that it’s helped you friends
thanks for your time in giving this feedback.
Your explanation was tooo good
great explanation, thank you so much….
hi java4s team ,
you have given very excellent explaination , i got it clearly .
thanks a lot …
Thankx a lot for providing this and happy with your explanation
simple and straight…
simple and powerful example. thanks a lot
its well explained loving it
you xplanation is powerful so much, i think i m now java professional ::D thank u for explanetion also do u know dynamic-update? is good for scaling webapp? 8) lol
Hi ,
I am reffering to the above comments about the code explaination:
“Hear s1 is in detached state, and at line number 11 we modified that detached object s1, now if we call update() method then hibernate will throws an error, because we can update the object in the session only”
If we call merge() method instead of update also some Exception will occur, And in the second session we can again use the session2.update(s1) instead of session2.merge(s1) ?? isn’t it ?? Then what is the basic difference that where to use update and where to use merge ?? …..
Kindly clarify …..
Thanks in advance
Navin
Great explanation. I used to always confuse read and confused again but this explanation has cleared my all doubts. Explanation is friendly, easy to understand and direct not like typical examples found in books, tutorials that server nothing.
Hey Navin might be helpful
Sivateja has explained very well
But if you wanted in depth, try to look at this article
http://www.stevideter.com/2008/12/07/saveorupdate-versus-merge-in-hibernate/
Generally we refer more than one site for our doubts, with this explanation no need of that.. TQ Siva….
clearing concepts with simplicity! Thanks…….:)
I am sorry. I am not happy with this explanation .
My question is what is the need of loading s2 if we had to update s1 in the second session. This is intensionally done to throw exception. Give me a real senario when such things happen.I can’t find any.
Secondly if merge() method is enough to do the change in object what is the need of update()? I would prefer mege() always instead of update().
Why the update() method is there?
the major difference between update and merge is that update cannot be used when the same object exists in the session.
Example :-
Student current = (Student)session.get(Student.class, new Integer(1));
System.out.println(“before merge ” + current.getName());
Student changed = new Student();
changed.setId(1);
changed.setName(“Changed new Name”);
// session.update(changed); // Cannot Use Update Since same object (Student) with Id 1 already exists
session.merge(changed); // merge will work fine and the name will get changed.
System.out.println(“after merge ” + current.getName()); // here current will now print the latest changed value since when the merge occurrs the one loaded in session is changed