Why dont container just compare the public fields of my Primary key class to
search for entity. Why it relies on equals and hashcode. ??
----- Original Message -----
From: "Cedric Beust" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, June 25, 2002 10:28 AM
Subject: Re: hashcode and equals method


> From: A mailing list for Enterprise JavaBeans development
> [mailto:[EMAIL PROTECTED]] On Behalf Of Ashwani Kalra

> hi,
> I have few questions on overriding the hashcode and equals
> method in primary key class
>
> 1. If I dont implement these methods properly then what are
> the problems I can face.

It depends how bad you implement them :-)

It is very important to get equals() right.  It is easy to get it right,
it is harder to get it efficient.  To get it right, you simply need to
respect the rules given in the Javadoc at

http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html#equals(java.
lang.Object)

If you fail to get it right, your EJB container will probably reward you
with numerous ObjectNotFoundException as it keeps failing to find your
Entity beans through their primary keys.

To get it efficient, you can refer to the emails that Juan and I just
exchanged, or some longer threads that have taken place on
theserverside.

hashCode() is a little different since it only affects performance.
Basically, the poorer your hash method is, the more collisions will
occur when the container is storing the primary keys in its internal
structure.  The end result is probably longer cache access and other bad
consequences (this is implementation-dependent).  Same remark as above,
there is plenty of literature on the subject, both from an EJB
perspective, or more generally, on perfect hash functions.

A rule of thumb:  xoring (^ in Java) the respective hashCodes() of your
primkey-fields usually yields a decent hash function.  It doesn't hurt
to experiment, though, you sometimes get very interesting results when
you start investigating how your hash maps get filled.

> 2. What is the best way to implement them.
> 3. Do I always need to be careful or can I be liberal
> in certain applications
> 4 I have read that any implementation what ever is not foolproof.

I hope to have given you enough information to answer these questions.
I would definitely recommend leaving the generation of these functions
to tools (well, good ones :-)).

--
Cédric

=========================
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".

===========================================================================
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".

Reply via email to