Gotcha: nHibernate NonUniqueObjectException

Quick note on a gotcha. Was getting the infamous NonUniqueObjectException: a different object with the same identifier value was already associated with the session from NHibernate.

Typically, this error occurs when you are using objects across NHibernate ISession’s. In my case, I was using the same ISession but was still receiving the error when I added two New objects to a parent object’s collections.
After running through “there must be another session being created” thought over and over, I finally discovered that the ID mapping for the child object did not have a generator assigned. I’m guessing this was causing NHibernate not to realize that when id equaled 0 it was transient. In my implementation, I just included logic to always make sure the child object’s identity is unique and allow the database to resolve the actual identity for the next session.

An interesting red herring that I’m glad to be rid of.

Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: