-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hashcode Computations
PostPosted: Tue Nov 15, 2005 8:32 pm 
Newbie

Joined: Tue Nov 15, 2005 8:27 pm
Posts: 19
Location: Columbia, MO USA
I have some questions concerning the instructions on pp. 124-125 of Hibernate in Action where it talks about business key equality.

1. What is the recommended way to compute hashcodes when there is more than one property in the business key? Is it recommended to use an implementation like that on page 124? If so, how were the numbers "14" and "29" chosen?

2. I can see where two objects with the same business keys will always return the same hashcodes. Conversely, how concerned should I be about the possibility that two objects with different business keys could map to the same hashcode on rare occasions?

3. I understand that business keys should change "rarely" and that they should not be changed while the domain object is in a set. Is it acceptable (for example to handle cases where the user wants to correct a typo in a person's name) to first remove the object from the set, make the change, and then add it back to the set? Are there other situations that I need to watch for when a component property of a business key changes?

Thanks,

Bruce Alspaugh


Top
 Profile  
 
 Post subject: Re: Hashcode Computations
PostPosted: Wed Nov 16, 2005 4:13 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
alspaughb wrote:
1. What is the recommended way to compute hashcodes when there is more than one property in the business key? Is it recommended to use an implementation like that on page 124? If so, how were the numbers "14" and "29" chosen?


If you use HibernateTools reverse engineering you will get something like:
Code:
   public int hashCode() {
         int result = 17;
         
         result = 37 * result + getBezBankenverfahren();
         result = 37 * result + getNrMandant();
         result = 37 * result + ( getDatenstichtag() == null ? 0 : this.getDatenstichtag().hashCode() );
         result = 37 * result + (int) getNrGeschaeft();
         result = 37 * result + getUnterNrGeschaeft();
         return result;
   }   

Here you have 17 and 37. The number are more or less arbitrary.
You just need to add some chaos, so that the hashkeys are unique
in most cases.

alspaughb wrote:
2. I can see where two objects with the same business keys will always return the same hashcodes. Conversely, how concerned should I be about the possibility that two objects with different business keys could map to the same hashcode on rare occasions?

A HashMap works in two phases:
1. First select a table with the hashkey,
2. go through that table using the equals method.
This last step guarantees that you will never have collisions.
A bad designed hashCode method only results in a bad performance.
Look up the implementation of HashMap.

alspaughb wrote:
3. I understand that business keys should change "rarely" and that they should not be changed while the domain object is in a set. Is it acceptable (for example to handle cases where the user wants to correct a typo in a person's name) to first remove the object from the set, make the change, and then add it back to the set? Are there other situations that I need to watch for when a component property of a business key changes?


I am not sure about this. Just try it.


Hope I could help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.