简体   繁体   English

如何在Google App Engine _ah / api / explorer中创建com.google.appengine.api.datastore.Key对象?

[英]How to create the com.google.appengine.api.datastore.Key object in the Google App Engine _ah/api/explorer?

I have a GAE JDO annotated object: 我有一个带有GAE JDO注释的对象:

@PersistenceCapable
public class HelloGreeting {

  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key id;

  @Persistent
  private String message;

  ... constructors, getters, setters
}

and my own endpoint: 和我自己的端点:

public HelloGreeting update(HelloGreeting helloGreeting)
        throws NotFoundException {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        pm.getObjectById(HelloGreeting.class, helloGreeting.getId());
        ... some logic
    } finally {
        pm.close();
    }
    return helloGreeting;
}

where I would like to send from frontend or from the Google App Engine _ah/api/explorer the helloGreeting object. 我想从前端或Google App Engine _ah / api / explorer 发送helloGreeting对象的位置。

When I create one using pm.makePersistent(helloGreeting); 当我使用pm.makePersistent(helloGreeting);创建一个 , then the api explorer shows me something like this: ,然后api资源管理器向我显示了以下内容:

{
 "id": {
   "kind": "HelloGreeting",
   "appId": "project-id",
   "id": "5629499534213120",
   "complete": true
  },
"message": "some text"
}

I use then this JSON structure to call the update(HelloGreeting helloGreeting) method via the api explorer and I'm getting the following error: 然后,我使用此JSON结构通过api资源管理器调用update(HelloGreeting helloGreeting)方法,但出现以下错误:

java.lang.NullPointerException at com.google.appengine.api.datastore.Key.getAppId(Key.java:279)
...
...

... which was because of line pm.getObjectById(HelloGreeting.class, helloGreeting.getId()); ...这是由于pm.getObjectById(HelloGreeting.class, helloGreeting.getId()); in my update(HelloGreeting helloGreeting) method. 在我的update(HelloGreeting helloGreeting)方法中。

It seems like the private Key id field is not unmarshalled correctly. 似乎私钥ID字段未正确解组。 I tried it also with pm.getObjectById(HelloGreeting.class, helloGreeting.getId().getId()); 我也尝试过pm.getObjectById(HelloGreeting.class, helloGreeting.getId().getId()); but with no success. 但没有成功。

Can somebody please tell me if this is even possible? 有人可以告诉我这是否可能吗?

PS: I can't change the type of the id field from Key to Long (which works) because then I wouldn't use inner child object annotated with @PersistenceCapable . PS:我无法将id字段的类型从Key更改为Long (这是可行的),因为那样的话我就不会使用带有@PersistenceCapable注释的内部子对象。

我不确定您的情况,但我认为您可能需要编写自定义转换器来确保正确重构密钥。

I solved this with a bit nasty solution. 我用一个讨厌的解决方案解决了这个问题。 This is my update() endpoint: 这是我的update()端点:

public HelloGreeting update(HelloGreeting helloGreeting)
        throws NotFoundException {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        if(!containsGreeting(helloGreeting)) {
            throw new NotFoundException("not exists");
        }
        // persisting the helloGreeting with the new Key
        pm.makePersistent(helloGreeting);
    } finally {
        pm.close();
    }
    return helloGreeting;
}

private boolean containsGreeting(HelloGreeting helloGreeting) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    boolean contains = true;
    try {
        // here I'm creating a new Key from the helloGreeting
        Key key = KeyFactory.createKey(HelloGreeting.class.getSimpleName(), helloGreeting.getId().getId());
        // using the Key for a query
        pm.getObjectById(HelloGreeting.class, key);
        // and rewriting the old incorrect unmarshalled Key by the new one 
        helloGreeting.setId(key);
    } catch (javax.jdo.JDOObjectNotFoundException ex) {
        contains = false;
    } finally {
        pm.close();
    }
    return contains;
}

If somebody knows a better solution, I would appreciate it. 如果有人知道更好的解决方案,我将不胜感激。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 App Engine JDO Persistent类从com.google.appengine.api.datastore.Key迁移到Long - App Engine JDO Persistent class migrate from com.google.appengine.api.datastore.Key to Long Google App Engine:java.lang.ClassCastException:com.google.appengine.api.datastore.Key 无法转换为 java.lang.Integer - Google App Engine: java.lang.ClassCastException: com.google.appengine.api.datastore.Key cannot be cast to java.lang.Integer Google Appengine-ID为“ com.google.appengine.api.datastore.Key:Product(“ Potatoe”)”的对象由其他对象管理器管理 - Google Appengine - Object with id “com.google.appengine.api.datastore.Key:Product(”Potatoe“)” is managed by a different Object Manager 从字符串生成密钥(com.google.appengine.api.datastore.Key) - Generating a Key (com.google.appengine.api.datastore.Key) from string 如何将Google App Engine数据存储区中的数据写入com.google.appengine.api.datastore.Text - How do I write data in my Google App Engine Datastore to com.google.appengine.api.datastore.Text Google App Engine-java.lang.NoClassDefFoundError:com.google.appengine.api.datastore.DatastoreServiceFactory - Google App Engine - java.lang.NoClassDefFoundError: com.google.appengine.api.datastore.DatastoreServiceFactory Google App Engine:java.lang.NoClassDefFoundError:com / google / appengine / api / datastore / Query $ Filter - Google App Engine: java.lang.NoClassDefFoundError: com/google/appengine/api/datastore/Query$Filter 示例应用中的com.google.appengine.api.datastore.DatastoreNeedIndexException错误 - com.google.appengine.api.datastore.DatastoreNeedIndexException error in Sample App 如何在数据库中存储“ com.google.appengine.api.datastore.Text” - How to store “com.google.appengine.api.datastore.Text” in database 如何使用com.google.appengine.api.datastore.Text - how to use com.google.appengine.api.datastore.Text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM