简体   繁体   中英

update vs merge method in hibernate

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.

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 .

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

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.

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 ie 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 ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM