Difference between merge and update methods in hibernate

(433 Views)


Difference between hibernate merge vs update method

Merge and update methods reside inside the Session interface provided by Hibernate. A Hibernate object has three different states, that are transient, persistent and detached and the methods (merge and update) convert the detached state object into persistence state. Now let's see each method in detail.

Update():

When we call the update() method, an enquiry is made whether the object exists in session cache or not. If this updating object is present in the session cache, it throws a NonUniqueObjectException exception, else it updates the object.

We'll be working on our Student table. Present state of our table:
Difference between update and merge methods in hibernate Now we try to update the name at id 2.
Configuration config = new Configuration(); config.configure("hibernate.cfg.xml"); SessionFactory factory = config.buildSessionFactory(); Session session = factory.openSession(); Student student = (Student)session.get(Student.class, 2); session.close(); //Since we've closed the session, student object is in detatched state, update can take place student.setName("Krishna Murti"); //attatching student to new session Session session2 = factory.openSession(); Transaction t = session2.beginTransaction(); session2.update(student); t.commit();
Output table state:
merge vs update in hibernate

Now let's see an example which raises NonUniqueObjectException:

Session session = factory.openSession(); Student student1 = (Student)session.get(Student.class, 5); session.close(); student1.setName("new Name"); //student1 is in detatched state //To update we need to have it in session Session session2 = factory.openSession(); Student student2 = (Student)session.get(Student.class, 5); //student2 is the same object as student1 Transaction t = session2.beginTransaction(); session2.update(student1); t.commit(); //Exception will be thrown as session2 already has the same object

Merge():

Merge() method like update() also change objects state from detatched to persistent.

When we call merge() method, a query is made whether the object exists in the session or not. If the object exist in the cache, then changes are copied into the cache else it loads the values to cache.

Here's a merge() snippet:
Session session = factory.openSession(); Student student1 = (Student)session.get(Student.class, 1); session.close(); student1.setName("new name"); Session session2 = factory.openSession(); Student student2 = (Student)session2.get(Student.class, 1); Transaction t = session2.beginTransaction(); session2.merge(student1); t.commit();

Conclusion:

  1. In update() method, if the object already exists in Cache, then update() method will throw an Exception.
  2. In merge() method, if the object already exists in Cache, then merge() method will simply copy the object into Cache.

Solution Worked 3 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search




Search Tags