<p>Is there any way to save an object using Hibernate if there is already an object using that identifier loaded into the session?</p>

<ul>
<li>Doing <code>session.contains(obj)</code> seems to only return true if the session contains that exact object, not another object with the same ID.</li>
<li>Using <code>merge(obj)</code> throws an exception if the object is new</li>
</ul>

[{'answer_id': 26468, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 2, 'selected': False, 'text': '<p>Have you tried calling .SaveOrUpdateCopy()? \nIt should work in all instances, if there is an entity by the same id in the session or if there is no entity at all. This is basically the catch-all method, as it converts a transient object into a persistent one (Save), updates the object if it is existing (Update) or even handles if the entity is a copy of an already existing object (Copy).</p>\n\n<p>Failing that, you may have to identify and .Evict() the existing object before Attaching (.Update()) your "new" object.\nThis should be easy enough to do:</p>\n\n<pre><code>IPersistable entity = Whatever(); // This is the object we\'re trying to update\n// (IPersistable has an id field)\nsession.Evict(session.Get(entity.GetType(), entity.Id));\nsession.SaveOrUpdate(entity);\n</code></pre>\n\n<p>Although the above code could probably do with some null checking for the .Get() call.</p>\n'}, {'answer_id': 40768, 'author': 'Georgy Bolyuba', 'author_id': 4052, 'author_profile': 'https://Stackoverflow.com/users/4052', 'pm_score': 1, 'selected': False, 'text': '<p>How about:</p>\n\n<pre><code>session.replicate(entity, ReplicationMode.OVERWRITE);\n</code></pre>\n\n<p>?</p>\n'}]
['https://Stackoverflow.com/questions/26450', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2875/']