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

This is from section 10.7. Automatic state detection of the bernate Reference Documentation.

This is from section 10.7. Automatic state detection of the bernate Reference Documentation: saveOrUpdate() does the following: if the object is already persistent in this session, do nothing if another object associated with the session has the same identifier, throw an exception if the object has no identifier property, save() it if the object's identifier has the value assigned to a newly instantiated object, save() it if the object is versioned (by a or ), and the version property value is the same value assigned to a newly instantiated object, save() it otherwise update() the object and merge() is very different: if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance the persistent instance is returned the given instance does not become associated with the session, it remains detached You should use Merge() if you are trying to update objects that were at one point detached from the session, especially if there might be persistent instances of those objects currently associated with the session. Otherwise, using SaveOrUpdate() in that case would result in an exception.

Thank you. Essentially I was looking for that reference documentation, I googled it for a while and couldn't find it! Thanks!

– EvilSyn Oct 4 '08 at 22:45 good answer... I wonder - if I use merge on a new entity is there any reason to use save afterwords or I can assume merge has created the new entity in the DB for sure? (and if it's an detached entity, once merge is the changes omitted to the DB automatically? ) – Dani Nov 30 '09 at 12:30 4 Are you sure about this?

Looking at the Nberante source SaveOrUpdateCopy triggers a Merge event with the same parameters as the Merge function. I think the they are identical, the SaveOrUpdateCopy function is something that has existed in hibernate/nhibernate since 1.0 the Merge function is new and was added to to hibernate to conform to a new java standard (I think) – Torkel Jan 4 '10 at 9:04 3 @Torkel - SaveOrUpdateCopy isn't the same as SaveOrUpdate. I'm not sure if the questioner wanted to compare Merge the former or the latter.

SaveOrUpdateCopy is a now-obsolete method which did a merge in Nbernate prior to Merge being imported. – codekaizen Apr 13 '11 at 0:01 good to know ... SaveOrUpdate is still heavily used in tutorials. – anael Apr 13 '11 at 15:08.

As I understand it, Merge will take an object that may not be associated with the current session, and copy its state (property values, etc. ) to an object that is associated with the current session (with the same PK value/identifier, of course). SaveOrUpdate will call Save or Update on your session, based on a given object's identity value.

SaveOrUpdateCopy() is now deprecated as of N1. Merge() should be used instead.

It's SaveOrUpdateCopy which is marked Obsolete, not SaveOrUpdate. There appears to be a lot of confusion between these two different methods in this question and subsequent answers. – codekaizen Apr 12 '11 at 23:59.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions