Here's a simplified class:
class Foo implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Set<Foo> children;
public Foo( Integer id ) {
if( id == null ) {
throw new IllegalArgumentException( );
}
this.id = id;
this.children = new HashSet<Foo>( 16 );
}
@Override public int hashCode( ) {
return id.hashCode( );
}
...
}
As you can see, it contains the set by itself and uses the property idto generate the hash. But I have a problem when an object has a self-referencing loop:
When an object is de-serialized, processing begins first with the deepest object, and then builds back. This is usually fine, but if an object contains one of the higher objects in its own children, it sets it: when it tries to add this object to its HashSet, it calls hashCodebut has idnot been loaded for this object yet, so it crashes with an exception NullPointerException.
(It took me a while to trace this!)
, : ? id ( -) children.