users@glassfish.java.net

Re: JPA, equals and hashCode

From: <glassfish_at_javadesktop.org>
Date: Fri, 14 Dec 2007 12:08:38 PST

WIth option 2 you still probably don't want to use the @Id field in hashCode. @Id is normally set when you initially save an object, which means that hashCode will change at this point. Confusion will ensue.

I normally use business keys in both hashCode and equals, but add @Id in equals. This is complex, but correct.

Furthermore: With any option, I have regrettably found it necessary to do an "instanceof" test instead of the recommended "getClass() == getClass()" test (from Effective Java). This is to support the subclassing that happens with lazy loading.
[Message sent by forum member 'jhannes' (jhannes)]

http://forums.java.net/jive/thread.jspa?messageID=250199