-->
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.  [ 7 posts ] 
Author Message
 Post subject: Question about contains() and containsAll() on Hibernate Set
PostPosted: Fri Aug 20, 2004 3:08 pm 
Newbie

Joined: Fri Aug 20, 2004 2:48 pm
Posts: 4
2.1.4

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

Debug level Hibernate log excerpt:

Before I do this, I just need to ask if this is the expected behaviour.
I have one to many mapping, as a Set in the parent. If I
retrieve the same
parent twice, and get two Sets of children (one from each parent) -
which should obviously be logically the same, then use
contains() on one Set with a child from another set, should
it not return true as they both contain same children?
I am getting false, for all elements. I have defined equals()
method on the
children (which are the elements of the set) as "business key"
equals (so it doesn't use the id) and if I manually
compare two elements from different sets, the equals() does
return true. This is all within the same session.
I can post more info but I wanted to know if this is how it
should be before going through all that (and I was unable to
find any kind of release notes on the website to see if this is
a known problem).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 20, 2004 3:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You forgot to define hashCode correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 20, 2004 5:14 pm 
Newbie

Joined: Fri Aug 20, 2004 2:48 pm
Posts: 4
Wow, thanks! That indeed was the problem! I was adding hashCodes()
to several classes and forgot to add it to this one. It now works!

I would like to ask a question for my own education though.
My understanding of hash code
and its role goes back to C so it may well not be how it works now.
Because this function just overrides the one built into each Java
object, wouldn't the lack of it just cause the default function the
return the same hash code for each
object, which would only lead to (drastically) reduced performance
as instead of elements being spread accross the whole hash map,
they would end up all piled up in a big linked list in the same
slot (my description would apply to a hash map, not to a set though)? How does the lack of hashCode() actually cause it not to
function at all?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 20, 2004 5:32 pm 
Newbie

Joined: Fri Aug 20, 2004 2:48 pm
Posts: 4
Nevermind, I was told that this is a Java thing (you've got to write
a custom hashCode() anytime you write a custom equals()).


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 21, 2004 8:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Take a look here: http://www.geocities.com/technofundo/tech/java/equalhash.html


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 21, 2004 8:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
The default hashCode() implementation does not return the same hashcode for every object, it returns a hashcode dependant on the memory location. This conforms to the default equals() which just does a ==


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 23, 2004 2:29 pm 
Newbie

Joined: Fri Aug 20, 2004 2:48 pm
Posts: 4
Thank you, that is a very useful link. It doesn't actually mention that hashCode() is based on memory location, which is a useful thing to know and it now makes perfect sense. These things are so easy to forget if you don't use them daily.

Additionally, the link shows how to do the equals() method and I was
able to learn something new - i.e. that using instanceof is not a prefered
way of doing it. This one's getting bookmarked.


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