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).