This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Why hashCode() and equals() should be implemented in PrimaryKey class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Reply Bookmark "Why hashCode() and equals() should be implemented in PrimaryKey class" Watch "Why hashCode() and equals() should be implemented in PrimaryKey class" New topic
Author

Why hashCode() and equals() should be implemented in PrimaryKey class

great veera
Greenhorn

Joined: May 14, 2001
Posts: 24
Hi ranchies,
can anyone tell me why the hashCode() and equals() should be implemented in primarykey class.I have gone thro' the ejbmatrix pdf it says these 2 methods are recommended and should be implemented.
And moreover i 've gone thro' oreilly's link http://www.oreilly.com/catalog/entjbeans/chapter/appd.html
which says
the new specification requires the primary key class to implement the Object.equals() and Object.hashCode() methods to ensure that these methods evaluate properly when comparing keys and storing them in a java.util.Hashtable .....
if this is the case why the keys should be compared and stored in Hashtable..
Pl anyone clarify me.
TIA
veera
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Entity beans are identified by their primary key. Not only from the perspective of the EJB developer, but also from the perspective of the EJB container.
Think for example of the findByPrimaryKey() method in the home interface. The container needs an efficient way to search for the primary key you've given, to see if it already has an entity bean for that key in the instance pool. If it finds the key, it needs to retrieve the actual bean. All of this has to be a completely generic, as the primary key may be any Java class.
If you know your Collections framework, this description just screams the word "HashMap". For the HashMap to work, the key object needs to implement hashCode() and equals().
- Peter

[This message has been edited by Peter den Haan (edited May 21, 2001).]
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
Hi Peter,
That was a very good answer to a very good Q but i would like to know more. Cud you pass on a URL for my benefit and for all of the rest.
Thanks
Sahil


Adios
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
Yes PETER, i hope you can pass to us the URL. Thanks...


Be Afraid...Be very Afraid...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why hashCode() and equals() should be implemented in PrimaryKey class
 
Similar Threads
How to identify whether decorator pattern is used by looking at API
Difference between Statement and PreparedStatement
How to count ?
Why override equals() method?
serialized transportation