ContactUs

Please send your Questions & Answers or Feedback to "mohan@javabook.org"

What is merge method ?

merge :
    Student student1 = (Student)session.get(Student.class,"1"); == P
   
    Student student2 = new Student();
    student2.setStudentNo("1");
    student2.setStudentName("venu"); == D
   
    session.update(student2);
  
it will throw...NonUniqueObjectException --  a different object with the same identifier value was already associated with the session...
Note : student1 object is there in the session with identifier 1 and while calling update we are trying to update with the same identifier....so the conflict will come...
    To solve this issue use merge() instead of update()....
Copy the state of the given object onto the persistant object with the same identifier.If there is no persistant currently associated with the session,it will be loaded.Return the persistance instance.if the given instance is unsaved,save a copy of and return it as a newly persistant instance.The given instance does not become associated with the  session.


Its not possible to keep 2 objects with the same identifier in the session.merge will not copy the object to the session,it will execute the insert/update directly.
when we try to keep 2 different objects with the same id value,the update()throws an Exception,to overcome this problem use the session.merge() which directly communicate with the DB without associating the object with the session.But update always associate the object with the session.
Related Posts Plugin for WordPress, Blogger...
Flag Counter