On Sun, Oct 11, 2009 at 10:49 AM, Ray Krueger <raykr...@gmail.com> wrote:
> Yeah, it's even worse :P
> 1.6.0_14 Enum.hashcode() on my ubuntu box returns something random every time.
> 1.6.0_16 Enum.hashcode() on my Macbook Pro here returns a consistent
> hashcode on every round.
>
> 1.6.0_14 on Ubuntu implements Enum.hashCode() using super.hashCode()
> 1.6.0_16 on the Mac implements Enum.hashCode() using System.identityHashCode()
>
> I'm updating java on my ubuntu box to see if 1.6.0_16 changes that
> implementation.
> It'd be interesting if Apple fixed it and Sun didn't.
The implementation of Enum.hashCode() is effectively the same (in
theory). It'd be interesting if Apple fixed Enums in the JVM native
code and Sun didn't.
Let me try a larger sample on my MBP...
Amazing. Yep, the apple JDK returns the same value every single time.
Yeah, that's what I'm doing it, starting a new instance over and over.
You might get better performance by pre-calculating the hashcode in
the Enum constructor as in my example, rather than calculating it on
every invocation.
Probably a trivial difference if there is one at all. The JVM can
probably optimize it out.
*shrug*
Glad we figured that out though, that's awful.
Insanity! heh well hopefully you don't run into this. It should really
only come up if you're using composite key objects with Enums in their
value and calculating hashcodes.
In my experience I stopped implementing equals() and hashcode() on my
Hibernate domain objects a long time ago. It leads to nothing but
trouble. And of course, not bashing Jason here, don't ever use
composite keys unless you're stuck integrating with some legacy
database.
If your domain objects use surrogate IDs the hibernate Type system
will always use the ID as the hashcode() vaue of the CacheKey object.
On a composite key it seems it uses the real hashcode of the PK
object.