-->
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.  [ 6 posts ] 
Author Message
 Post subject: hashcode & equals automation?
PostPosted: Sat Sep 13, 2003 6:04 am 
Newbie

Joined: Sat Sep 13, 2003 5:48 am
Posts: 6
Location: Hannover, Germany
Hi all,

I just wondered whether there's a helper class or an other mechanism in hibernate which calculates hashCode() values and performs the equals()-method.

If not, take my question as a feature request.
It shouldn't be too difficult to build since hibernate has all needed information about the objects properties in its mapping files and already makes strong use of reflection.

Furthermore all information about the consideration of detail objects in equals() and hashCode() can be retrieved from the hibernate mappings, too. I.e. the question whether the hashcode of associated detail objects shoud be included in an objects hashcode can be answered by the cascade-attribute of the association mapping.

Bye, Stefan


Top
 Profile  
 
 Post subject: Re: hashcode & equals automation?
PostPosted: Sat Sep 13, 2003 6:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
stefan.prange wrote:
Hi all,

I just wondered whether there's a helper class or an other mechanism in hibernate which calculates hashCode() values and performs the equals()-method.

If not, take my question as a feature request.
It shouldn't be too difficult to build since hibernate has all needed information about the objects properties in its mapping files and already makes strong use of reflection.

Furthermore all information about the consideration of detail objects in equals() and hashCode() can be retrieved from the hibernate mappings, too. I.e. the question whether the hashcode of associated detail objects shoud be included in an objects hashcode can be answered by the cascade-attribute of the association mapping.

Bye, Stefan


hbm2java has a simple equals and hashcode generation.

and yes hbm.xml contains much information that can be used to perform this - but remember that writing automated equals and hashcode can be hard since sometimes you don't have all the properties set - e.g. when you are using assigned ids...

Any patches and strategies for doing this the right way is welcome!

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2003 6:38 am 
Newbie

Joined: Sat Sep 13, 2003 5:48 am
Posts: 6
Location: Hannover, Germany
Thanks for your quick reply. I really appreciate that your working on weekends (just like I'm doing now ;-)

I didn't think of generated code for the both methods.
I thought of helper methods which examine an objects properties online.

I just learned that the correct implementation of hashCode() and equals() is crucial for hibernate collections to work.
So I have to implement them thoroughly. Since I'm a lazy guy I'll try to delegate these tasks to a helper object which might use the net.sf.hibernate.persister.ClassPersister class.
Of course I don't know yet if it will work but if it does I'll post the code here.

Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2003 7:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You could implement something like this using Hibernate's metadata API. However, think carefully about the philosophical implications of making your domain model dependant upon Hibernate classes!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2003 7:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
gavin wrote:
You could implement something like this using Hibernate's metadata API. However, think carefully about the philosophical implications of making your domain model dependant upon Hibernate classes!


...and performance!

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2003 7:26 am 
Newbie

Joined: Sat Sep 13, 2003 5:48 am
Posts: 6
Location: Hannover, Germany
Quote:
think carefully about the philosophical implications of making your domain model dependant upon Hibernate classes


Well, that has already happended because all classes of my business model implement the interface net.sf.hibernate.Lifecycle in order to observe the persistence state of an object (is it new, is is changed, stored or deleted?) I implemented a common super class for this purpose.
BTW: Is there a better solution for this problem?

I didn't even start with my hashCode-equals-helper class. I think the most difficult problem is the handling of associated objects so I might omit a solution in the first shot.
Maybe I will totally fail and write 16 (the number of my business classes) hashCode-equals-pairs by hand. We'll see...


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