0

I created Key with my unicue ID, and save into the database.

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

everything works!

enter image description here But know I have one problem.

In order to get my Object From Db I must need Key string.

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

how to create that key? I think it is enycrypted key. How can I now get this object?

P.S I think because of I have one to many relationship, that may does not work. Because When I create that pdf without 1:N it gets object very well. But I cant get another datas, which are in 1:N

I have that error:

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")
4

2 回答 2

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);
于 2013-10-18T06:13:40.370 回答
0

解决方案是无主的:

@Unowned
@Persistent
private Map <String,GoogleDrivePDF > pdfs;
于 2013-11-11T08:34:48.703 回答