JPA_and_Hibernate

Update vs Merge in Hibernate

Posted On
Posted By admin

In this blog post, I will be comparing the Update and Merge methods in Hibernate. I will be explaining how they are similar. I will also be explaining the differences between them.

 

Similarities

Both update and merge methods on the session interface. Both methods update a record in the database. Both move an entity from the detached state to the persistent state.

Differences

The following table lists the differences between the update and merge methods:

Update Merge
Hibernate proprietary methodPart of the JPA specification
Does not return anything, returns a voidReturns the updated object
Makes the original object persistent. So if the original object is changed after invoking update, changes will get saved when the session is flushedDoes not make the original object persistent, so even if the original object is changed after merge, changes will not get saved in the database. Instead, the merged object is tracked for changes
Always performs an SQL updateOnly performs an SQL update if the fields of the object passed in are different from the fields in the database

 

 

 

 

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my Hibernate course Hibernate from scratch

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment