> From: A mailing list for Enterprise JavaBeans development
> [mailto:[EMAIL PROTECTED]]On Behalf Of Satyabrata Dash
> I believe that we override hashcode() to ensure same hashcode for the same
> primary key everytime the container calls it. It is essential because each
> entity bean maps to a single row in database. So it is essential that the
> same primary key is evaluated to same hashcode everytime. Container maintains
> the identity of the entity bean in a hashtable where hashcode and primary key
> are key, value pairs. that is ensured by overriding the hashcode() method.
> Again we override equals() method to find out whether the primary key is same
> or not.
Technically, you don't *have* to override hashCode(). The default Object
implementation will work fine. However, this will most likely result in poor
performances because a lot of collisions will occur in hashtables where the
primary key is stored.
A good Java rule of thumb is: "if you need to implement either equals() or
hashCode() in your class, then you should implement both". This principle should
be carried over to primary keys. Also, it makes sense that both methods use the
same fields in their implementation.
Note that there was a similar principle in C++ for copy
ctor/operator=()/destructor.
--
Cedric
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".