java - Changing hashCode of object stored in hash-based collection -


i have hash-based collection of objects, such hashset or hashmap. issues can run when implementation of hashcode() such can change time because it's computed mutable fields?

how affect hibernate? there reason why having hashcode() return object's id default bad? not-yet-persisted objects have id=0, if matters.

what reasonable implementation of hashcode hibernate-mapped entities? once set id immutable, it's not true moment of saving entity database.

i'm not worried performance of hashset dozen entities key=0. care whether it's safe application , hibernate use id hash code, because id changes generated on persist.

if hash code of same object changes on time, results unpredictable. hash collections use hash code assign objects buckets -- if hash code changes, collection doesn't know, can fail find existing object because hashes different bucket now.

returning object's id isn't bad, if many of them have id=0 mentioned, reduce performance of hash table: objects same hash code go same bucket, hash table no better linear list.

update: theoretically, hash code can change long nobody else aware of -- implies @bestsss mentioned in comment, remove object collections may holding , insert again once hash code has changed. in practice, better alternative generate hash code actual content fields of object rather relying on database id.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -