-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: business keys
PostPosted: Tue Dec 28, 2004 10:46 pm 
Newbie

Joined: Tue Dec 28, 2004 10:38 pm
Posts: 1
On page 125 you suggest using item_id and bid amount as the business key for the Bid class. What if the particular item you are referring to has not yet been made persistant???? No item_id will exist????
I'm new to all this, please excuse any silly questions. This technology you have created has a lot of appeal from what I've read so far.

Tim


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 12:17 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Then you can't use the id of the Item, of course. The examples purpose was to show that you can indeed sometimes use the id of an associated entity, if you can guarantee that it is already persistent. This is usually easy, esp. if you think about items and bids; no bids will be made until an item has been saved.


Top
 Profile  
 
 Post subject: equals and hashCode vs item_id
PostPosted: Mon Jan 02, 2006 6:42 am 
Newbie

Joined: Mon Jan 02, 2006 6:12 am
Posts: 6
Ask the question "does the relating object allways exists in the database prior to saving this object?". If the answer is yes then you can use the relating objects id to implement equals and hashCode. If the answer is no, then you will have to use the relating objects equals and hashCode method instead.

yes:
Code:
public int hashCode() {
  int result = 14;
  result = 29 * result + getBidAmount().hashCode();
  result = 29 * result + getItem().getId().hashCode();
  return result; 
}


no:
Code:
public int hashCode() {
  int result = 14;
  result = 29 * result + getBidAmount().hashCode();
  result = 29 * result + getItem().hashCode();
  return result;
}


You can allways use the relating objects equals and hashCode method instead of the id but the result will be that the relating object (the item for the bid object) will have to be loaded into the session to use equals and hashCode. If the relating object equals and hashCode relies on a relating object, the result can be that a large number of relating objects will have to be loaded to calculate a single equals or hashCode. This will of cause effect performance.

Peter


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.