pindleskin wrote:
I met the same problem, a solution I could find out for now is: after save/update the parent object which has persistent set, use session.refresh(parentObject) to force hibernate to reload the parent object and re-construct the presisent set(so the hashcodes are re-calculated).
I must second this.
Suppose you have a "Set<Foo> fooSet" and try to remove "bar" (which is of class Foo).
If you iterate over the set using a foreach and then checking each individual result with .equals you will find it if your equals / hash methods are implemented.
Code:
for (Foo f : fooSet) {
if(bar.equals(f) {
System.out.println("Found 'bar' !");
}
}
This does not mean however that you will always find it using set.contains(object). If set.contains(object) doesn't find the object in question
you will not be able to remove it via set.remove(object), even if the previoulsy mentioned iteration over the set finds your object via equals.
Code:
if (!fooSet.contains(bar)) {
System.out.println("Object 'bar' not found");
}
So the foreach finds your object in question, the contains does not.
I think this is outrageous to be able to find it via iteration but not via contains !.
I agree with the above poster that this is most likely because of hashCode not being updated correctly.
And for everone saying "Well implement equals / hash correctly":
I know of 109.html and not everyone can use a distinct business key which uses
"semi"-unique attributes.
And for all who don't have a unique business key, there seems to not be a nonbroken approach:
http://forum.hibernate.org/viewtopic.php?t=928172
Sorry for ranting but I think this behaviour of PersistentSet is unacceptable (especially in combination of NOT noting anythign about this in the contains Method of the ersistentSet-Javadoc)
cheers,
JSFUser