Hibernate version: 2.1.6
I am having a hard time with those NonUniqueObjectExceptions.
I have a detached object instance that I want to reattach. In some cases there's already another instance of the object in the current session, so that I get the NonUniqueObjectException. That's quite correct behaviour.
Unfortunately I did not find any way how to check whether the another instance of the detached object is already present in the cache, because session.contains() always returns false.
I debugged into this and found out that contains() does the following:
Code:
return entityEntries.containsKey(object);
Finally this ends in HashMap.containsKey(), which calls HashMap.hash(), which calls HashMap$IdentityKey.hashCode().
And here's the real problem:
Code:
public int hashCode() {
return System.identityHashCode(key);
}
This method calls identityHashCode, which totally omits any hashCode() implementation in my Hibernate persisted beans. Thus session.contains() will always return false, except if the detached object and the instance that is contained in the session have the same identityHashCode, which is impossible.
Maybe I am wrong, but isn't this a major problem with the Hibernate implementation.....