I'm scratching my head with an exception thrown intermittedly when saving objects with NHibernate 2.0-2.0.1.
All in all it's very similary to this:
http://forum.hibernate.org/viewtopic.php?p=2396478
I'm posting again not to hijack the thread. Perhaps this contains further clues.
The context is
- NH 2.0 or 2.0.1
- ASP.NET
What I'm thinking at this point is that the exception only occurs when:
- using MySQL
- saving "copies" of objects (I'll post more details further down)
- cascading inserts of mapped entities
- (SQL server and SQLite seem to work fine)
So far my debugging spree has led me to this place in the NH codebase:
Code:
AbstractSaveEventListener::PerformSaveOrReplicate
- source.ActionQueue.Execute(insert);
It appears my entity goes in with id 0 and comes out with an identity already used by another entity. When I venture further down the inserts and identity selects looks okay as far as I can tell.
This is an exceprt of my HBM:
Code:
...
<map name="Details" inverse="true" cascade="all-delete-orphan" generic="true" where="DetailCollectionID IS NULL" lazy="true">
<cache usage="read-write" />
<key column="ItemID" />
<index column="Name" type="String" />
<one-to-many class="N2.Details.ContentDetail, N2"/>
</map>
...
<class name="N2.Details.ContentDetail,N2" table="n2Detail">
<cache usage="read-write" />
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<discriminator column="Type" type="String" />
...
This is my Equals and GetHashCode:
Code:
public override bool Equals( object obj )
{
if( this == obj ) return true;
ContentDetail other = obj as ContentDetail;
return other != null && id == other.id;
}
public override int GetHashCode()
{
if (id > 0)
return id.GetHashCode();
return base.GetHashCode();
}
What about that copying? I've previously used memberwiseclone and cleared specific fields (such as the details collection). I've also tried to create a new object using activator and assigning specific fields to no effect.
Other oddities include a not completly deterministic execution. I wouldn't swear by it but it seems timing related.
I'll probably continue banging my head against this for a while but I'd gladly accept hints of any kind. Thank you for your time.