-->
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: Hibernate Set class
PostPosted: Fri Feb 06, 2004 2:52 am 
Newbie

Joined: Thu Jan 29, 2004 8:59 pm
Posts: 3
I read in the doc that the Hibernate libraries actually implement it's own version of Set. I'm wandering if this implementation's contains(Object o) method calles .equals() on the members in the Set (like it is meant to according to the definition of the contains(Object o) function).

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 3:03 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes, we also constantly remember people to implement equals()/hashCode() correctly.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Set class
PostPosted: Sun Feb 08, 2004 11:15 pm 
Newbie

Joined: Thu Jan 29, 2004 8:59 pm
Posts: 3
christian wrote:
Yes, we also constantly remember people to implement equals()/hashCode() correctly.


What is the correct way to implement the equals()? Are there any common problems people run into when using it? My problem is that calling set's contains() doesn't call equals() in the objects in Set.

thanks
Stephane


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 5:41 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes, we have an outdated text on the wiki that says "implement equals using the persistent identifier". This is not a good design, use the "semi"-unique attributes of your persistent class to implement it. For example, if I have an "Item" class and it has a "name" String and "created" Date, I can use both to implement a good equals() method.

The combination of both fields is stable enough for the duration an Item may be in a Set. It is not as good as a primary key, but it's certainly a weaker candidate key. Don't use the persistent identifier (the primary key) for equals(), as it will not have a value assigned if an object is new.

Calling contains() calls equals() on the objects in the Set. If it doesn't, your Java is broken. ;)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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.