-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with equals method from hbm2java
PostPosted: Sat Dec 13, 2003 4:35 pm 
Newbie

Joined: Sat Nov 08, 2003 7:57 pm
Posts: 2
Location: Georgia
The equals method gen'd by hbm2java includes the primary key in the equals test. This makes sense for an object that has been persisted.

However, it prevents adding several new instances of a class to a set (Map) prior to saving.

I have hacked around it by commenting-out the gen'd equals method.

How should this be handled?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 14, 2003 7:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
first - this is tool related and should be in the tools forum instead ;)

Secondly - yes, equals() is pretty hard to implement on objects which identity changes while inside a Collection since that is illegal according to the Collection API ;(

...I've not yet came up with a good solution for it....one way is just to not override equals and thus fallback on '=='-comparisons instead.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 1:19 am 
Regular
Regular

Joined: Tue Sep 02, 2003 5:09 pm
Posts: 81
Location: Whitefish Montana
Depending on the implementation, you should be able to change an object in a Collection but you should not change an object in place in a Set. That means that you cannot change the key for your Map. I am assuming this occurs for a mapping that has a native id. I haven't used maps very often with Hibernate but I have ran into the same problem sevaral times with a Set. I get around it by changing the equals test to include a candidate key and if there is no candidate key I use all columns except the id if the id is not set. Remember to keep your hashCode method in sync with equals so that equal objects always return the same hash code.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 3:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
If you use all columns or the key then the check will most likely change when in a set....won't it ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 1:36 pm 
Regular
Regular

Joined: Tue Sep 02, 2003 5:09 pm
Posts: 81
Location: Whitefish Montana
That hasn't been a problem in practice because I usually build up the object, add it to the set, and commit. I am not sure how hibernate avoids the problem when it sets the id from the natively generated key but it hasn't caused any errors.


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