Hi,
I have a class defined with a UniqueIdentifier ID in SQL Server. I am using the Guid generator in my mapping file. The default value is {000...
This is a web application where NHibernate sessions are opened and closed per round trip. I am storing the object being inserted/modified in a Session variable (so it retains its attributes on round trips)
There is another attribute of this class which I have defined as a unique index in the database.
If I attempt to insert a new object into the database using SaveOrUpdate and an error occurs due to an index violation, a new identity of the object is assigned by NHibernate anyways.
This becomes problematic, because after the identity is assigned, the user is notified and can change the unique attribute to a different value, but upon trying to resave, NHibernate tries to perform an update operation as opposed to an insert operation. This is because the ID has been set and NHibernate thinks that the object is already in the database.
Is there any way around this aside from clearing the falsely assigned ID in my object?
|