google-code-export / objectify-appengine

Automatically exported from code.google.com/p/objectify-appengine
MIT License
1 stars 0 forks source link

loading @Embedded Set with POJOs implementing hashcode/equals fails with NPE #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
@Embedded
private Set<Foobar> foobars = new HashSet<Foobar>();

public class Foobar implements Serializable {
    private Date date;

    public int hashCode() {
        return date.hashCode();
    }

    public boolean equals(Object obj) {
        return date.equals(((Foobar)obj).date);
    }

}

When I try to load this parent pojo I get:
java.lang.NullPointerException
        at com.example.model.Foobar.hashCode(Foobar.java:54)
        at java.util.HashMap.put(HashMap.java:372)
        at java.util.HashSet.add(HashSet.java:200)
        at com.googlecode.objectify.impl.load.EmbeddedMultivalueSetter.safeSet(EmbeddedMultivalueSetter.java:90)
        at com.googlecode.objectify.impl.load.CollisionDetectingSetter.set(CollisionDetectingSetter.java:37)

I think this happens because of the following code in EmbeddedMultivalueSetter:
    Object embedded = TypeUtils.newInstance(this.getComponentConstructor());
    embeddedMultivalue.add(embedded);

It creates a new Foobar using the default constructor and then adds it to the 
collection (Set) which in turn calls hashcode which cannot execute because all 
properties of Foobar are null at the moment.

Original issue reported on code.google.com by ioannis....@gmail.com on 23 Nov 2010 at 2:39

GoogleCodeExporter commented 9 years ago

Original comment by lhori...@gmail.com on 6 Dec 2010 at 10:07

GoogleCodeExporter commented 9 years ago
I think the issue is even more severe. When items are inserted into HashSet 
their hashcode is calculated right away (from HashSet point of view they are 
immutable). If later on these items are updated (in such a way that their 
hashcode is affected) the hashcode is not recalculated by HashSet and these 
updated items effectively cannot be found in HashSet anymore.

Original comment by mp...@yahoo.com on 26 Jan 2011 at 3:53

GoogleCodeExporter commented 9 years ago

Original comment by lhori...@gmail.com on 26 Jan 2011 at 6:52

GoogleCodeExporter commented 9 years ago
In some cases a workaround can be utilized by making the Set @Serialized, 
however you won't be able to query on any of the fields.

Original comment by cspeckru...@gmail.com on 17 Mar 2011 at 7:40

GoogleCodeExporter commented 9 years ago
I have added a test case and hopefully will have this fixed by EOD today.

Original comment by lhori...@gmail.com on 17 Mar 2011 at 8:42

GoogleCodeExporter commented 9 years ago
This is fixed in trunk.

Original comment by lhori...@gmail.com on 18 Mar 2011 at 12:19