R – How Do I copy an existing nhibernate object as a new object

netnhibernate

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new?

I am thinking I might create a session interceptor to force every entity to look new and ensure the id is left blank like a new unpresisted entity would.

Best Answer

You can implement the Clone method in your class. It is up to you to decide how to do this (swallow or deep copy). The identity property must be set to the 'unsaved-value' declared in the mapping file, so that NHibernate knows it is a new object.