These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Exceptional NonUniqueObjectExceptions
PostPosted: Thu Dec 18, 2008 8:04 pm 
Newbie

Joined: Thu Jan 04, 2007 3:56 pm
Posts: 11
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 6:04 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You may run into problems with that:

Code:
public override int GetHashCode()
      {
         if (id > 0)
            return id.GetHashCode();
         return base.GetHashCode();
      }


The hash code changes after the object gets an id, which happens during save. Try to use a constant hash code.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 3:48 pm 
Newbie

Joined: Thu Jan 04, 2007 3:56 pm
Posts: 11
I tried removing the GetHashCode override (which should revert to reference based calculation) but it didn't help.

Is it a bad idea to use an incremental ID fto calculate the hash code?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2008 9:04 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have look here:

http://nhforge.org/blogs/nhibernate/archive/2008/09/06/identity-field-equality-and-hash-code.aspx

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.