When using Primary Keys, why do I have to implement the hashCode() and equals() method in my bean?

Ryan Lynch

Implementing the hashCode() and equals() functions ensure that the primary key object works properly when used with hash tables. Hash tables are the preferred way EJB servers use to store and quickly retrieve instantiated entity beans.

If session #1 uses widget "A" (which is an entity bean) then the server needs to do some work to instantiate and initialize the object. If session #2 then requests the same widget "A", the EJB server will look in its hash table of existing entity beans of type widget to see if widget "A" has already been instantiated.