Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between Hibernate Session methods saveOrUpdate() and merge()?

Tags:

java

hibernate

What are the differences between Hibernate Session methods saveOrUpdate(Object) and merge(Object)? It seems that both methods generate an SQL INSERT statement when the given object does not exist in the corresponding table and an UPDATE when it does exist.

like image 586
Derek Mahar Avatar asked Dec 16 '10 15:12

Derek Mahar


3 Answers

Even though this is for NHibernate, it also applies to Hibernate:

NHibernate - Difference between session.Merge and session.SaveOrUpdate?

like image 98
jpkrohling Avatar answered Oct 02 '22 03:10

jpkrohling


If I remember correctly, merge is for un-associated instances, that aren't currently managed. It will do a lookup based on id and merge the two.

like image 31
Scott Avatar answered Oct 02 '22 04:10

Scott


SaveOrUpdate versus Merge in Hibernate explains the differences between saveOrUpdate() and merge().

like image 21
Derek Mahar Avatar answered Oct 02 '22 03:10

Derek Mahar