Thread:
 Can't we store the hashcode in the database? 
 jonthe   20 Apr 2007, 11:32 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: jonthe (20 Apr 2007, 11:32) Replies: 0, Views: 7929
Subject: Can't we store the hashcode in the database?
I started out a big project using the approach recommended on this page
with an equals method based on business properties and a hashcode
corresponding to this. Yesterday my world crashed down upon me because
of strange errors. But hey I broke the hashcode/equals contract when I
changed properties of objects in a Set so I can only blame my self. 

Now the easiest solution I have come up with is the following:

add a field int hashcode to my objects and persist it with the object.
At creation time instatiate the field as a random number. In the equals
method check for both business property equality and hashcode equality.
(This is needed since the hascode is not an unique identifier of the
object). And the hascode()-method simply returns the stored hashcode.

Only downside I can figure out is that it is impossible to create two
equal objects other than by cloning, but that really shouldn't be a big
problem, and the saving of the hascode to the database but harddrives
are cheap and it ain't that big. Anyone see any other problems?
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]