You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Difference Between Merge And Update Methods In Hibernate

Hibernate » On Sep 12, 2011 By Sivateja

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

Explanation

  • See from line numbers 69, 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
  • here 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.

Folks i have been changed the values in the source code

Difference-between-merge-and-update-methods





By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja [at] java4s.com
Follow me on


Comments

37 Responses to “Difference Between Merge And Update Methods In Hibernate”
  1. Sanket says:

    Nice explanation i m surprise there is no comment for this this explanation cleat most of my doubt Thanx

  2. Java4s says:

    @Sanket

    You bet :-), thanks for your feedback on this.

  3. jaffar says:

    Your’s explanation is like flow of river in calm state.I am glad to see your site here from your code.

  4. Java4s says:

    @Jaffar

    Thanks much :-) Glad to hear your feedback.

  5. Rajesh says:

    Its fabulous and great way of explaining things, I just love it.

  6. Harish says:

    I was always confused with these two methods but your way of explanation is simply awesome. Now I will never forget the difference. Thanks !!

  7. Java4s says:

    @Rajesh,@Harish

    I am really happy that it’s helped you friends :-) thanks for your time in giving this feedback.

  8. Nagarjuna says:

    Your explanation was tooo good

  9. Amey says:

    great explanation, thank you so much….

  10. Raju Kapadne says:

    hi java4s team ,
    you have given very excellent explaination , i got it clearly .

    thanks a lot …

  11. srinivas says:

    Thankx a lot for providing this and happy with your explanation

  12. sudhakar says:

    simple and straight…

  13. chinna says:

    simple and powerful example. thanks a lot

  14. amar says:

    its well explained loving it

  15. Sharif says:

    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

  16. NAVIN says:

    Hi ,

    I am reffering to the above comments about the code explaination:

    “here 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

  17. raghu says:

    yes same question i have please reply to Navin query .

  18. 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.

  19. 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/

  20. karthik says:

    Generally we refer more than one site for our doubts, with this explanation no need of that.. TQ Siva….

  21. pankaj says:

    clearing concepts with simplicity! Thanks…….:)

  22. Debasis says:

    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?

  23. mani says:

    what is the difference between save and persistence

  24. 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

  25. uj says:

    hey navin
    i am completely agree with your explanation and you are right it will throw an exception..
    The main difference between update and merge is ..when we call session.upadate() in session2 where object become persistence object from deacted object it looks for any update before and after session2.update then save it in dataBase but when you call session.Merge() it look at the update before the session.merge() stmt it doesn’t take care of stmt after session.merge() as session.update take care and update the dataBase..sorry if i am wrong

  26. ashokkumar says:

    Explain about Fetching Strategies?

  27. Murali KB says:

    Article doesn’t explain difference between merge and update? Explanation from Sumeet Chopra in the comments session is better. Thanks.

  28. divya says:

    Amazing sir..
    clear cut explanation.

  29. Monika says:

    Nicely explained.

  30. Satish says:

    Very nice explanation. Thanks :)

  31. Sujata says:

    Merge – First fetches the entity then does update.
    Update – Updates the entity directly.
    In both the cases it doesnot matter if entity already exists in the session or not.

  32. Nice explanation. Thank you.

  33. Vishnu says:

    when iam starting a tomcat server in eclipse, it started but browser will display an error like “requested resource is not found” even for admin console also.

  34. Doubt!!!!After loading S2 you are not doing anything with this object. You are just merging the S1 object with session2. What is going inside hibernate? How hibernate executing the queries?

  35. tarkesh says:

    Student s2 = null;
    Object o1 = session2.get(Student.class, new Integer(101));
    s2 = (Student)o1;

    i m not understand what is the requirement of this code
    can you explain me pls

  36. samba says:

    Great.Very good explanation.Thank you…

  37. Selva says:

    Amazing explanation.Thank you very much

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ In Progress ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.