【问题标题】:How to create the com.google.appengine.api.datastore.Key object in the Google App Engine _ah/api/explorer?如何在 Google App Engine _ah/api/explorer 中创建 com.google.appengine.api.datastore.Key 对象?
【发布时间】:2015-12-19 19:14:43
【问题描述】:

我有一个 GAE JDO 注释对象:

@PersistenceCapable
public class HelloGreeting {

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

  @Persistent
  private String message;

  ... constructors, getters, setters
}

还有我自己的端点:

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;
}

我想从前端或 Google App Engine _ah/api/explorer 发送 helloGreeting 对象。

当我使用pm.makePersistent(helloGreeting); 创建一个时,api explorer 会显示如下内容:

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

然后我使用这个 JSON 结构通过 api explorer 调用 update(HelloGreeting helloGreeting) 方法,我收到以下错误:

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

...这是因为我的update(HelloGreeting helloGreeting) 方法中的pm.getObjectById(HelloGreeting.class, helloGreeting.getId()); 行。

似乎 private Key id 字段未正确解组。 我也尝试了pm.getObjectById(HelloGreeting.class, helloGreeting.getId().getId());,但没有成功。

有人可以告诉我这是否可能吗?

PS:我无法将 id 字段的类型从 Key 更改为 Long (有效),因为那样我就不会了t 使用带有 @PersistenceCapable 注释的内部子对象。

【问题讨论】:

    标签: java google-app-engine google-cloud-endpoints jdo


    【解决方案1】:

    我不能 100% 确定您的情况,但我认为您可能需要编写自定义 transformer 以确保正确重建密钥。

    【讨论】:

    • 我害怕这个答案:D
    【解决方案2】:

    我用一个有点讨厌的解决方案解决了这个问题。这是我的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;
    }
    

    如果有人知道更好的解决方案,我将不胜感激。

    【讨论】:

      猜你喜欢
      • 2011-05-23
      • 2019-04-26
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-18
      • 2021-06-07
      • 1970-01-01
      相关资源
      最近更新 更多