سؤال

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")
هل كانت مفيدة؟

المحلول 2

Solution was Unowned:

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

نصائح أخرى

If you want to create a Key based on some other field of your Entity (say, for instance, a field called uniqueID), you should do something like this:

Persisting:

GoogleDrivePDF pdf = new GoogleDrivePDF();
pdf.setUniqueID("0B1IQEoiXFg3IWHhJNEtlMzlvQWs");
Key key = KeyFactory.createKey(GoogleDrivePDF.class.getSimpleName(),pdf.getUniqueID());
pdf.setKey(key);
pm.makePersistent(pdf);

BTW, it is generally not a good choice to make the key field publicly accessible. I think it is better to put the code for the creation of the key inside a specific constructor, like

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

Retrieving:

String idToBeRetrieved = "0B1IQEoiXFg3IWHhJNEtlMzlvQWs";
Key key = KeyFactory.createKey(GoogleDrivePDF.class.getSimpleName(),idToBeRetrieved);
pm.getObjectById(GoogleDrivePDF.class, key);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top