Today I faced this ancient evil… and it almost destroyed me.
This exception comes out when you try to save or update an object with the same id of another object that is already in the scope of the NHibernate session.
My scenario was this: I had to save the instance of an object. Well, I have to choose if use a Insert or an Update. To do so, I query the db in search of an object with the id of mine and act accordingly.
BUT, the query keeps in the session the instance of the object and the next Update operation fails with the NonUniqueObjectException. What to do? Well, in this case you have to send in update the same object the GetByID returns to you.
So:
1) Query the db and keep the instance of the object A it returns (if any)
2) Update A with the data you store in the original updated object B
3) Save A instead of directly save B
and you’ll defeat the Evil NonUniqueObjectException!



