GAE使用带有密钥的JDO获取数据

时间:2013-10-17 16:20:53

标签: google-app-engine transactions google-cloud-datastore jdo google-api-java-client

我用我的unicue ID创建了Key,然后保存到数据库中。

 GoogleDrivePDF pdf = new GoogleDrivePDF();
 key= KeyFactory.createKey(GoogleDrivePDF.class.getSimpleName(),"0B1IQEoiXFg3IWHhJNEtlMzlvQWs");
 pdf.setKey(key);           
 pdf.setPdfName("NAME");

一切正常!

enter image description here 但是知道我有一个问题。

为了从Db获取我的对象,我必须使用Key string。

 GoogleDrivePDF pdf  = pm.getObjectById(GoogleDrivePDF.class,   "agtsdHYtY2hlY2tlcnJoCxIRVXNlcnNQREZEb2N1bWVudHMiIXZha2h0YW5nLmtvcm9naGxpc2h2aWxpQGdtYWlsLmNvbQwLEg5Hb29nbGVEcml2ZVBERiIcMEIxSVFFb2lYRmczSVdIaEpORXRsTXpsdlFXcww");

如何创建该密钥?我认为这是enycrypted的关键。我现在怎么能得到这个对象?

P.S我认为因为我有一对多的关系,这可能不起作用。因为当我创建没有1:N的pdf时,它很好地得到了对象。但是我无法得到另一个数据,它们是1:N

我有这个错误:

Could not retrieve entity of kind GoogleDrivePDF with key GoogleDrivePDF("0ByMb_8ccYJRFOS1ibmFtamZ1b2M")
org.datanucleus.exceptions.NucleusObjectNotFoundException: Could not retrieve entity of kind GoogleDrivePDF with key GoogleDrivePDF("0ByMb_8ccYJRFOS1ibmFtamZ1b2M")

2 个答案:

答案 0 :(得分:1)

如果您想根据实体的其他某个字段创建密钥(例如,名为uniqueID的字段),您应该执行以下操作:

<强>坚持:

GoogleDrivePDF pdf = new GoogleDrivePDF();
pdf.setUniqueID("0B1IQEoiXFg3IWHhJNEtlMzlvQWs");
Key key = KeyFactory.createKey(GoogleDrivePDF.class.getSimpleName(),pdf.getUniqueID());
pdf.setKey(key);
pm.makePersistent(pdf);
顺便说一下,将key字段公开访问通常不是一个好的选择。我认为最好将用于创建密钥的代码放在特定的构造函数中,比如

public GoogleDrivePDF(String uniqueID) {
    this.setUniqueID(uniqueID);
    this.key = KeyFactory.createKey(GoogleDrivePDF.class.getSimpleName(),uniqueID);
}

<强>检索:

String idToBeRetrieved = "0B1IQEoiXFg3IWHhJNEtlMzlvQWs";
Key key = KeyFactory.createKey(GoogleDrivePDF.class.getSimpleName(),idToBeRetrieved);
pm.getObjectById(GoogleDrivePDF.class, key);

答案 1 :(得分:0)

解决方案是无主的:

@Unowned
@Persistent
private Map <String,GoogleDrivePDF > pdfs;