What is the difference between and merge and update

Questions by rajneeshg   answers by rajneeshg

Editorial / Best Answer

promisingram  

  • Member Since Oct-2008 | Oct 19th, 2008


Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session. In other words, update() is usually the first method you would call in a fresh session, ensuring that reattachment of your detached instances is the first operation that is executed.

Showing Answers 1 - 4 of 4 Answers

chandra

  • Jul 24th, 2007
 

In COBOL merging means combining records of two or more sequential files. This can be acheived through MERGE VERB. After merging all the records can be stored in seperate output file. In output file all the records are sorted.
Update means we can modify the records of any file. This can be achieved through REWRITE VERB.

  Was this answer useful?  Yes

Hi
         Merge: merge is like combining records from more than one table(while  retreving   records from tables baseb on some conditions)


Update: Update is like edit . use to change the valu of record...

  Was this answer useful?  Yes

Update():- if you are sure that the session does not contains an already persistent instance with the same identifier,then use update to save the data in hibernate

Merge():-if you want to save  your modificatiions at any time with out knowing abot the state of an session, then use merge() in hibernate.

omkar5136

  • Aug 17th, 2008
 

Merge:Suppose if we take two words like.. one is milk, two is van
if we merge this two words it forms milkvan.

Update:Means, if we have know something about JAVA language, there are soo many new things are being added to the java language, if we know all or some portion of new things with existing things are taken as update.

  Was this answer useful?  Yes

Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session. In other words, update() is usually the first method you would call in a fresh session, ensuring that reattachment of your detached instances is the first operation that is executed.


jyothi_4

  • Feb 12th, 2011
 

merge() adds the pojo class object to the first level cache.First it checks in the database whether a given primary key value is available in the DB or not. If it is available it updates the data for that primary key. If it is not available it inserts the data into DB.


update() updates the record in the DB for the given primary key value.


merge() is similar to saveOrUpdate().

  Was this answer useful?  Yes

Consider empid as 1 and some emp data is already present in database. So it is called as detached state.
When we apply session.update (empobject) in this situation we get detached state to
persistent state and updated object is placed in database using update statement.

Consider the same thing with merge().
Emp e1=new Emp();
Emp e2=new Emp();
e1.setEno(new Long(1));
e1.setEname("updated ename"):
e2=(Emp)session.get(Emp.class,new Long(1));
session.update(e1);//exception NonUniqueObjectException
so for the above statement NonUniquesObjectException occurs.since e2 object is having emp object with same empid as 1.In order to avoid this exception we are using merge like given below instead of session.update(e1);

session.merge(e2);
session.update(e1);

  Was this answer useful?  Yes

Rajani

  • Oct 20th, 2011
 

In Hibernate session can maintain only one object in persistent state with same id number. while converting a detached object into persistent,if already that session has a persistent object with the same id then Hibernate throws Exception whenever "Update()" method is called to reattach a detached object with a session.

we need to call "Merge()" method ,instead of "update()". So that HB copies the state changes from detached object into persistent object. So that we can say a detached object is converted into a persistent object.

  Was this answer useful?  Yes

amit

  • Jan 29th, 2013
 

suppose you have detached object (means after closing session persistence object becomes detached object) and you have done some changes in this object with help of setter method now you created new session here in new session if you will do session.update(detached object) it will give you exception but session.merge(detached object) will work fine .

  Was this answer useful?  Yes

Vanky

  • May 26th, 2015
 

This term is basically used in svn (storage repository)
Merge: Merging new code with existing one
Update: After Merging or starting new task its very imp. to take updated file of working copy

  Was this answer useful?  Yes

Mukesh Vishwakarma

  • Aug 25th, 2015
 

Update will work at same session. After close the session or detached the state, update will not work.
But in the case of Merge after closed the session it will work.

  Was this answer useful?  Yes

Thomas John

  • Sep 21st, 2015
 

I create an object say Employee with id 1. I create this object by getting the student record from the database. Object s1 is in the session
Employee e1 = null;
Object o = session1.get(Employee.class, new Integer(1));
e1 = (Employee)o;
If I say session1.close() then session1 will be destroyed and the object e1 will not be in the session1 anymore. Object e1 is in the detached state. If we call update() then hibernate will throw error because we can update the object in the session only.
Now I open another session session2,load the employee object from the database under a different object e2.
In session2, I will call session2.merge(session1). This will move the changes of e2 into e1
Object o1 = session1.get(Employee.class, new Integer(1));
e2 = (Employee)o1;
e2.setName("New Name");
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.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions