Thoughts from Dan Miser RSS 2.0
 Wednesday, March 11, 2009
I came across this exception today when inserting multiple objects through an NHibernate SessionScope:

NHibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0, of class: Foo.Entity.Bar
   at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IEntityPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
   at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
   at NHibernate.Impl.SessionImpl.Save(Object obj)

I didn't see much out there on this, but Geoff Lane picked up on it pretty quickly. It turns out that my Bar.hbm.xml mapping file had set the id generator incorrectly to "generated", which was a problem because it was actually a MSSQL IDENTITY field. The mapping should have looked like this:


<id name="Id">
  <generator class="native" />
</id>

Luckily for me, I had run my tests by trying to insert 2 entities inside the session so the error bubbled up right away.

Wednesday, March 11, 2009 8:06:02 PM (Central Standard Time, UTC-06:00)  #    Comments [0] -
.NET | ALT.NET
Comments are closed.
Navigation
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Dan Miser
Sign In
Statistics
Total Posts: 338
This Year: 4
This Month: 0
This Week: 0
Comments: 613
All Content © 2010, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)