【问题标题】:GAE get Data using JDO with keyGAE 使用带密钥的 JDO 获取数据
【发布时间】:2013-10-26 06:13:41
【问题描述】:

我用我的唯一 ID 创建了 Key,并保存到数据库中。

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

一切正常!

但知道我有一个问题。

为了从 Db 获取我的对象,我必须需要密钥字符串。

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

如何创建该密钥?我认为它是加密密钥。我现在怎样才能得到这个对象?

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

【问题讨论】:

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


    【解决方案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:N 关系时我有错误。没有 1:N 它可以工作。
    • 我知道如何从数据库中获取数据!但是当我有 1:N 时它不起作用!
    【解决方案2】:

    解决方案是无主的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 2013-09-22
      相关资源
      最近更新 更多