Thread:
 fallback to object identiy 
 hengels   16 Feb 2004, 16:02 
 Re: fallback to object identiy 
 RobJellinghaus   26 Feb 2004, 19:13 
 Re: fallback to object identiy 
 hengels   15 Mar 2004, 15:06 
 Re: fallback to object identiy 
 Mr_Light   31 Jan 2006, 13:56 
 Re: fallback to object identiy 
 iwein   22 May 2007, 05:12 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: iwein (22 May 2007, 05:12) Replies: 0, Views: 4217
Subject: Re: fallback to object identiy
POST QUESTIONS ON THE FORUM! COMMENTS HERE SHOULD ADD VALUE TO THE
PAGE!On 26 Feb 2004 19:13, RobJellinghaus wrote:

>On 16 Feb 2004 16:02, hengels wrote:

>>what about using the primary key and fall back to object identity if a
>>pk is not assigned?

>This breaks collections because it changes the object's hashcode once
>you save it.  So you would get:

...

>This *will* print "OOPS!" because newUser's hashcode changed when you
>saved it, so the userSet can no longer find it.

It seems to me that you're assuming a 'wrong' implementation of
hashCode() based on the pk. since we established that the pk may change
(be set) in equal objects hasCode() is not allowed to change if the pk does.

Still you can use pk in equals(). Just compare the pk if and only if
both objects have pk!=0. In case of 0 compare the business key (i.e.
some properties). This is based on the assumption that you will never
change properties that undermine equality once you've saved an object
(set it's pk) i.e. objects that have the same pk are always equal based
on their business key.
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]