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.
Why hashCode() and equals() should be implemented in PrimaryKey class
great veera
Greenhorn
Joined: May 14, 2001
Posts: 24
posted
0
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
posted
0
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
posted
0
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
posted
0
Yes PETER, i hope you can pass to us the URL. Thanks...