-->
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.  [ 1 post ] 
Author Message
 Post subject: net.sf.hibernate.collection.Set.equals() isn't reflexive
PostPosted: Mon Feb 05, 2007 6:00 pm 
Beginner
Beginner

Joined: Thu Dec 09, 2004 3:19 pm
Posts: 34
Hibernate version:
2.1.6

I have 2 instances of the same object recreated via Hiberante. Each has a Set of children objects and the sets aren't comparing equals, even though they should. More specifically, they compare equals() one diretion, but not the other, which violates equals() reflexive requirement.

I'm pretty positive its in the hibernate Set implementation, though it seems hard to believe others haven't hit it. Here's the code I used to isolate it to the Hibernate set implementation.

The first comparison uses the Hibernate set implementation. The second comparison creates new java.util.HashSet() implementations with the elements in the original sets. Hibernate's Set.equals() should return the same true/false for equals() as Java's since the contained elements are the same.

Code:
            System.out.println( "Inner Set impl: " + innerInstances.getClass().getName() );
            System.out.println( "Outer Set impl: " + outerInstances.getClass().getName() );
            System.out.println( "innerArr1=outerArr1: " + innerInstances.equals( outerInstances ) );
            System.out.println( "outerArr1=innerArry1: " + outerInstances.equals( innerInstances ) );
            System.out.println( "innerArr2=outerArr2: " + new HashSet(innerInstances).equals( new HashSet(outerInstances) ) );
            System.out.println( "outerArr2=innerArry2: " + new HashSet(outerInstances).equals( new HashSet(innerInstances) ) );


And here's the output:
Code:
Inner Set impl: net.sf.hibernate.collection.Set
Outer Set impl: net.sf.hibernate.collection.Set
innerArr1=outerArr1: false
outerArr1=innerArry1: true
innerArr2=outerArr2: true
outerArr2=innerArry2: true


I'd love to find out I'm doing something wrong, though the above seems pretty clear that Hibernate's Set implementation isn't checking equals() in the same way Java's is. Even if I had a bug in my object's equals() method, there's no reason why Hibernate and Java's Set.equals() should differ in results, right?

I've also got code (not shown here) that checks every object in the array to validate the equals() on the object is reflexive and the hashcodes for equal objects are the same. Everything points to the net.sf.hibernate.collection.Set implementation.

The only work around I can think of is to have all my equals() methods that compare children use the new HashSet( hibernateset ) trick I used above to validate the problem only shows in Hibernate's Set.

Thoughts? Suggestions? Is this a bug already found and fixed? (I looked in Jira, but didn't find anything matching it).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.