views:

890

answers:

3

I am using the Goole app engine datastore with Java and trying to load an Object with a List of Enums. Every time I load the object the List is null. The object is

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ObjectToSave {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

    @Persistent
    private List<AnEnum> anEnumList;

    //public getters and setters
}

The enum is simple

public enum AnEnum {
    VALUE_1,
    VALUE_2;
}

The code to save it is

ObjectToSave objectToSave = new ObjectToSave();
List<AnEnum> anEnumList = new ArrayList<AnEnum>();
anEnumList.add(AnEnum.VALUE_1);
objectToSave.setAnEnumList(anEnumList);
PersistenceManager pm = pmfInstance.getPersistenceManager();
try {
    pm.makePersistent(objectToSave);
} finally {
    pm.close();
}

The code to load it is

PersistenceManager pm = pmfInstance.getPersistenceManager();
try {
    Key key = KeyFactory.createKey(ObjectToSave.class.getSimpleName(), id);
    ObjectToSave objectToSave = pm.getObjectById(ObjectToSave.class, key);
} finally {
    pm.close();
}

I can view the data in the datastore using http://localhost:8080/%5Fah/admin and can see my List has been saved but it is not there when the object is loaded.

I created my project with the Eclipse plugin and haven't made any changes to the datastore settings as far as I know. So why id my Enum list null?

+1  A: 

How are you creating an instance of ObjectToSave? The default value of all instance variable reference types is null, so unless you have (additional) code to create an instance of List<AnEnum> and assign it to anEnumList, null would be expected.

Patrick Schneider
I am setting a value to the list with a debugger I can see its set before I save the object. I've updated the question to try and make this clearer.
Chris
Perhaps you need to mark your enum declaration as @PersistenceCapable?
Patrick Schneider
"A Collection of child objects (of @PersistenceCapable classes) creates multiple entities with a one-to-many relationship. See Relationships."From: http://code.google.com/appengine/docs/java/datastore/dataclasses.html#Collections
Patrick Schneider
The child objects "AnEnum" aren't PersistenceCapable, neither should they be. Enums are supported persistable types by JDO
DataNucleus
+3  A: 

Yes but your List field is not in the default fetch group at loading so hence is not loaded. Read JDO Fetch Groups. You could add it to the DFG, or enable a custom fetch group, or just "touch" the field before closing the PM.

--Andy (DataNucleus)

DataNucleus
A: 

Google App Engine does not support adding Collections like List to the Default Fetch Group. As Andy suggested you could touch the List (getAnEnumList()) to retrieve the data. If you're using GWT and intend to send the list over RPC you need to do a ObjectToSaveRPC=pm.detachCopy(ObjectToSave) or it will not pass as serialized.

Carl