-->
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.  [ 4 posts ] 
Author Message
 Post subject: hashcode() and equals()
PostPosted: Tue Mar 13, 2007 10:49 am 
Newbie

Joined: Tue Mar 13, 2007 10:36 am
Posts: 5
Hi,

I have one many-to-many association table DepartmentToUser with columns:

DepartmentID
UserID
PrimaryCode

Now I want to create the domain object for it and override the hashcode() and equals() method.

What properties should I use for that?

I think I can not use DepartmentID and UserID because they are set by Hibernate when the objects are saved. PrimaryCode alone is not unique. So it can not be used as Business Key.

I think my case is trivia; probably I am missing something since I am new to Hibernate.

Any kind of help will be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 11:26 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
http://www.hibernate.org/109.html

use whatever makes sense and will remain unchanged for the relative life of the object. But, primary keys generated by dbs aren't bad choices, if that is the only defining unique value.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 11:48 am 
Newbie

Joined: Tue Mar 13, 2007 10:36 am
Posts: 5
Hi kochcp,

Thanks for the reply.

I have a query regarding primary keys generated by dbs.

I have read in "Hibernate in Action" that these keys should not be used for hashcode() and equals() implementation.

Lets assume we are using DepartmentID and UserID to generate hashcode(). Now these properties are not set initially. But we add an instance of DepartmentToUser to a HashSet. Now we save the Department and User. So at that time IDs are set by Hibernate. So the hashcode changes for DepartmentToUser when it is contained by the Set. This should not happen.

Let me know your thoughts.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 1:36 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
oh, well if you're using a Set, then you have to use something else.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.