views:

50

answers:

2

I have existing entities of a type in my gae datastore to which i want to add a new primitive property of primitive type "long":

@Persistent     private long bests = 0;

When I do this, when i try to load existing entities which obviously don't have this property set yet, i get:

java.lang.NullPointerException: Datastore entity with kind Player and key Player("patrick") has a null property named bests. This property is mapped to model.Player.bests, which cannot accept null values.

What can I do to avoid this problem? Like a way to default to zero when field is not existent? I want to avoid using class Long, and stick with using primitive long.

+3  A: 

java type of Long accepts nulls, long doesn't. Existing data won't have that so are null.

DataNucleus
Is there a way to indicate that the field has been added and that a default value (such as 0) should be used if the data is missing from the underlying store?
Skip Head
No. JDO metadata provides an attribute that could easily be implemented to do just that (column default-value="..."), but sadly not implemented in Google's plugin. Also they don't seem active in doing any work on that plugin either, nor acting on issues raised on them in their issue tracker.
DataNucleus
I understand why the problem is happening, but I would like a good way to go around it without having to use object Long. I would like to stick with the primitive long.
Patrick
+1  A: 

You could use a Long temporarily, update all of your entities to have a value of zero, and then change the field back to a long. Alternatively, read in all of your data to some file, delete all of your entities, and write them back with the new long field (careful of ownership links being broken).

Riley
Thank you. This is the best alternative I have so far.
Patrick