Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between update() and merge() in Hibernate

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 4.66k
    Comment on it

    Difference between update() and merge() method :

    Hibernate has different methods to save, fetch, update and delete object. Commonly or the most popular methods to update the data are update() and merge(). The update() method is widely used to update the record while the merge() method is less, because very few users has knowledge about the merge() method. Some of programmers having confusions where to use update() and where should use the merge() method.

    In this article we will show you how update() is different from the merge() method. Difference between update() and merge() is as follows:

     

    1. update() and merge() both methods are used to update the db row if it already exist. But if object does not exist in db already then the update() method will throw the exception while merge() will create the new entry in db for that object.

    2. If session does not contains an already persistent instance with the same identifier and if you are sure about that then use update to save the data. But merge() method can save your modifications at any time with out having the knowledge about the state of session.


    For example :

    Session session1 = config.buildSessionFactory().getCurrentSession();
    
    Emplyoee emp1 = null;
    emp1 = (Emplyoee)session1.get(Employee.class, new Integer(105));
    session1.close();
    
    emp1.setAge(26);
    
    Session session2 = config.buildSessionFactory().getCurrentSession();
    Student emp2 = null;
    emp2 = (Emplyoee) session2.get(Emplyoee.class, new Integer(105));
    Transaction tx=session2.beginTransaction();
    
    session2.merge(emp1);
    

     

    In the above example at line number 4, we fetch an object emp1 from db into session1 cache and then closed session1 at line number 5.

    • Now emp1 object will be destroyed, not present in the session1 cache

    • Now emp1 object is in detached state, and at line number 7 we modified that detached object emp1, now if we call update() method this situation then hibernate will throws an exception, because we can not update the object outside the session.

    • So we opened another session [session2] at line number 9, and again loaded the same employee object from the database, but with name emp2.
    • So in this session2, we called session2.merge(emp1), and then the emp2 object and emp1 changes will be merged and saved into the database.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: