App engine does not store List when it is annotated with serializable and defaultFetchGroup set
I am trying to store an Jdo which has a 开发者_Python百科List in it. When the list is annotated with
@Persistent (defaultFetchGroup = "true")
It works fine, but when I have
@Persistent (serializable = "true", defaultFetchGroup = "true")
It does not store the list. Does anyone know why? What do I do when I have a list of custom objects that I want to store, do I mark them as serializable or not?
I see 3 cases :
- a core type (
List<String>
,List<Date>
, etc...) : You should use@Persistent
- a
@PersistenceCapable
class (List<MyPersistenceCapableClass>
) : You should use@Persistent
, it's the One-To-Many relationship case - not a core type neither a
@PersistenceCapable
class but a@Serializable
one (List<MySerializableClass>
) : You must use@Persistent (serializable = "true")
When using serializable = "true"
, you need to call JDOHelper.makeDirty
to notify JDO everytime you modify the list :
MySerializableClass serializableObject = new MySerializableClass();
persistedObject.getMySerializableClassList().add(serializableObject);
JDOHelper.makeDirty(persistedObject, "mySerializableClassList");
note : the defaultFetchGroup = "true"
parameter is used to tell JDO to automatically fetch all objects of the list, and not wait for access to the field (lazy fetching).
@Persistent(serialized = "true", defaultFetchGroup="true")
is the correct annotation.
Also when you are changing something to an element of the list you should call JDOHelper.makeDirty(this, "yourSerializedField");
because when it re-serialize the element it only looks at the reference not at each item (it uses .equals of the object)
Hope this helps
精彩评论