JDO basics: List or array won't retrieve or re

2019-09-25 01:54发布

I've been using Objectify up till now, but now I have to deal with some code using JDO. I'm having problems with basic stuff that is easy with Objectify, specifically: if an object has either a List or an array as a member, I can't get that to persist.

I have a class FileInfoBatch (code indents lost here, don't know why) containing a List of FileInfo:

@PersistenceCapable
public class FileInfoBatch implements Serializable{

private static final long serialVersionUID = 1L;        
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

@Persistent
private List<FileInfo> fileInfoList;    

@Persistent    
private String savedByUserEmail;

public FileInfoBatch(){         
    fileInfoList=new ArrayList<FileInfo>();     
}

Then I send a FileInfoBatch (called fib) containing just 1 FileInfo in the List, to my RPC impl class & persist it, then query for it straight away for a test (in debugger)

    PersistenceManager pm = PMF.get().getPersistenceManager();
    pm.makePersistent(fib);
    Query query = pm.newQuery(FileInfoBatch.class, "savedByUserEmail == '"+ userEmail + "'");
    List<FileInfoBatch> savedList = (List<FileInfoBatch>) query.execute();
    FileInfoBatch persisted=savedList.get(0);

The persisted FileInfoBatch comes back with an empty FileInfo list. If I use a single Fileinfo member, this persists ok.

I find no such basic problem reported on this site & I've looked at the docs: https://developers.google.com/appengine/docs/java/datastore/jdo/dataclasses

Does such a simple storage task require huge complexity? If so I will go back to using Objectify.

1条回答
男人必须洒脱
2楼-- · 2019-09-25 02:18

Define "doesn't persist"? Have you looked at the log (which tells you what is included in a PUT of an Entity) or the DB viewer for what is persisted? Is "FileInfo" marked as PersistenceCapable?

More than likely it is persisted, but you haven't paid attention to defining what is fetched (in JDO you get FULL control over what is in the retrieved object). Suggest that you read http://www.datanucleus.org/products/accessplatform_3_1/jdo/object_lifecycle.html and http://www.datanucleus.org/products/accessplatform_3_1/jdo/fetchgroup.html

Are you using a recent version of "datanucleus-appengine" ? (i.e v2.0 or later); the old versions aren't worth the effort.

PS, you don't need @Persistent on field types such as String, Key - makes things look much cleaner.

FWIW : JDO provides as simple a persistence choice as there is, as transparent as other options out there, and with semantics defined by a spec in way more detail than other options.

查看更多
登录 后发表回答