-->
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.  [ 1 post ] 
Author Message
 Post subject: Strategies for calculating hash codes from composite keys?
PostPosted: Fri Jul 23, 2010 6:21 pm 
Beginner
Beginner

Joined: Thu May 20, 2010 12:31 pm
Posts: 28
Are there any general collision-free best practices to generate hash codes for any (atomic) type composite primary keys?

I thought about it for a few hours and came to the conclusion, that a string concatenated by *all* primary key columns would be the only reliable way to do so. Calling Java's hashCode method on that concatenated string should yield a unique integer. (it would in fact somehow mimic what a database index does *eyerolling*)

Code:
    @Override
    public int hashCode()
    {
        String sSurrogate =   String.format("%011d", this.gameId)         // 11 chars
                            + String.format("%01d" , this.isHome ? 1 : 0) //  1 char
                            + String.format("%011d", this.playerId)       // 11 chars
                            + String.format("%011d", this.rosterId)       // 11 chars
                            + String.format("%06d" , this.period);        //  6 chars
       
        //System.out.println("surrogate = '" + sSurrogate + "'");
       
        return sSurrogate.hashCode();
    }


However, I don't believe my solution is optimal. There must be algorithms out there that are better. The above probably runs rather slow, but it's still faster than accessing a DB generator. What about standard hashing functions like MD5, SHA1, CRC, and Adler?

Karsten

PS: This is for overriding composite key ID classes...


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

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.