9

I understand that update is used to put a detached object into persistent state if no other object with the same id and type is attached to the session. Merge doesn't care about states. It just returns a persisted object of the same type if it doesn't exist in the session or it updates the old object with the values of the new object. My questions is regarding database hits. Does the method 'update' and 'merge' hit the database immediately? or changes are made apparent in the database when the session is closed.

Edit: What happens if we call the update method on a persisted instance by the save method?. I thought the update method was just used on detached instances.

Daniel
  • 119
  • 1
  • 2
  • 10

4 Answers4

26

Hibernate handles persisting any changes to objects in the session when the session is flushed. update can fail if an instance of the object is already in the session. Merge should be used in that case. It merges the changes of the detached object with an object in the session, if it exists.

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 modifications at any time with out knowing about the state of an session, then use merge() in hibernate.

When the entity instance is in the persistent state, all changes that you make to the mapped fields of this instance will be applied to the corresponding database records and fields upon flushing the Session. The persistent instance can be thought of as “online”, whereas the detached instance has gone “offline” and is not monitored for changes.

This means that when you change fields of a persistent object, you don’t have to call save, update or any of those methods to get these changes to the database: all you need is to commit the transaction, or flush or close the session, when you’re done with it. It is important to understand that all of the methods (persist, save, update, merge, saveOrUpdate) do not immediately result in the corresponding SQL UPDATE or INSERT statements. The actual saving of data to the database occurs on committing the transaction or flushing the Session.

Adya
  • 1,084
  • 9
  • 17
  • I agree with everything you said. You said: "Merge method will merge changes of both states of object and -->will save in database <---.". I highlighted my inquiry within two arrows in your answer. My question is when?. Are these changes saved in the database when the session is closed or as soon as the method is called. – Daniel Apr 02 '18 at 01:44
  • I have edited my answer, please see if that solves your query or not. – Adya Apr 02 '18 at 02:17
  • If hibernate actively tracks persistent objects for any update or changes and if there's any update is done then it automatically gets these changes into the database when flush/commit is called. Then why there's a need for an update method? – foxt7ot Jul 31 '21 at 04:41
  • What is a benefit of not saving to the database until you commit the transaction? – Collin Jan 28 '23 at 22:41
7

In case of merge: When we call merge method on detached instance, it will update it with updated value.

In case of update When we call update method on detached instance, it will give exception org.hibernate.NonUniqueObjectException

Sumit Singh
  • 111
  • 1
  • 5
3

All the methods in hibernate

  • save

  • merge

  • saveOrUpdate

  • Update

  • Delete

does not immediately result in sql update or insert statments.

The actual saving of data happens when we commit or flush the session.

Towkir
  • 3,889
  • 2
  • 22
  • 41
Nikhil Kamani
  • 850
  • 9
  • 12
0

Sometimes we face situation where we application database is modified with some external application/agent and thus corresponding hibernate entity in your application actually becomes out of sync with it’s database representation i.e. having old data. In this case, you can use session.refresh() method to re-populate the entity with latest data available in database.

You can use one of the refresh() methods on the Session interface to refresh an instance of a persistent object Method merge() does exactly opposite to what refresh() does i.e. It updates the database with values from a detached entity. Refresh method was updating the entity with latest database information. So basically, both are exactly opposite.

Merging is performed when you desire to have a detached entity changed to persistent state again, with the detached entity’s changes migrated to (or overriding) the database.

Hibernate official documentation give a very good explanation of merge() method:

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade=”merge”.

Reference

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
Vidya vid
  • 9
  • 1