-->
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.  [ 2 posts ] 
Author Message
 Post subject: Proper equals() implementation for sets?
PostPosted: Wed Feb 21, 2007 12:31 pm 
Newbie

Joined: Fri Dec 02, 2005 11:33 am
Posts: 5
Sounds kinda simple, but it has proven to be a tough nut to crack...

If we have a class pojo and a student pojo, the class has a set of students and the student has a set of classes (managed by separate class_student table), and both sets in the mapping files are defined as lazy.

How do you construct the equals() method on the pojos? I'm seeing that, when I add a student to the class, the set of students must be tested in the equals() method for hib to persist the relationship. The opposite is also true for the set of classes in the student.equals() method for hib to persist the relationship.

But now I get into a circular logic issue: client tests the set for equality, which tests each student, which in turn tests the classes which tests the students which tests the classes...

Then there's the lazy issue - if I have to test the set in the equals() method, the set must be populated and the whole reason behind lazy goes right out the window.

So what is the best way to deal with this kind of thing?


Top
 Profile  
 
 Post subject: RE: Proper equals() implementation for sets? -- DON'T
PostPosted: Wed Feb 21, 2007 2:37 pm 
Newbie

Joined: Thu Mar 03, 2005 3:25 pm
Posts: 5
So you have a Student and a Class. The equals methods should not rely on external relationships, or on mutable characteristics. Look for an natural candidate key instead (not a generated key) and us this for both hashCode() and equals().

Using mutable properties in the equals method for an object that can exist in a set is a big mess anyhow (particularly if the underlying implementation uses a Hash or similar). Look for best practices relating to equals() and hashCode() on google, then build both these methods using unique immutable properties. If you don't have any (and can't add them - say a student id or a course number), then use the hibernate version. If you do this, you have to 1) persist the objects before doing fun stuff with them (e.g. add them to sets) and 2) throw a runtime exception if the user trys to call either equals() or hashCode() without persisting the object.

Good luck.


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