Do any of you guys have seen this error?

I have a Parent entity that can have 0 or many children.

I my test case, in one session, I create the children (new records),
then persist the data, then I try to remove the children. The test is
trying to simulate different operations which normally get executed
one at the time in one request. Removing the children fails with the
dreaded 'deleted object would be re-saved by cascade' error.

It boils down to the fact that when I remove a child object from the
parent collection, the remove operation doesn't remove anything
because it doesn't find that item in the collection. I debugged
through the code and it ultimately uses this Dictionary function
called by the DictionarySet Contains function.

    private int FindEntry(TKey key)
    {
      if (key == null)
      {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
      }
      if (this.buckets != null)
      {
        int num = this.comparer.GetHashCode(key) & 0x7fffffff;
        for (int i = this.buckets[num % this.buckets.Length]; i >= 0;
i = this.entries[i].next)
        {
          if ((this.entries[i].hashCode == num) &&
this.comparer.Equals(this.entries[i].key, key))
          {
            return i;
          }
        }
      }
      return -1;
    }


 I think what's happening is this: when the children are added to the
parent Children collection, they are brand new, their ids are 0 and
their GetHashCode function depends on this id which now is 0. After
they are persisted their primary keys change to the database identity
fields, but then the values returned by GetHashCode function are
different than before when the ids were 0, hence finding these items
back in the collection gets messed up.

I am using llblgenpro, and the gethashcode function they generate in
the entity classes looks like this:

public override int GetHashCode()
{
  int toReturn = base.GetHashCode();
  toReturn ^= this.VariableId.GetHashCode();
  return toReturn;
}

The GetHashCode function is not immutable for the duration of the
object's life

In the java world the documentation states:

[Doc Start]
public int hashCode()

    The general contract of hashCode is:

        Whenever it is invoked on the same object more than once
during an execution of a Java application, the hashCode method must
consistently return the same integer, provided no information used in
equals comparisons on the object is modified. This integer need not
remain consistent from one execution of an application to another
execution of the same application.
[Doc End]

Clearly the implementation above violates this principle.

Any suggestions?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to