2

我是 appEngine 的新手,我尝试了我的项目所必需的简单事情:我创建了简单的 JDO 图像类:

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Image {

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

@Persistent
private Blob image;

@Persistent
private String type;

@Persistent
private String description;

public Key getKey() {
    return key;
}

public void setKey(Key key) {
    this.key = key;
}

public Blob getImage() {
    return image;
}

public void setImage(URL url) throws IOException {

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                url.openStream()));

        String line;
        StringBuffer stbf = new StringBuffer();

        while ((line = reader.readLine()) != null) {
            stbf.append(line);
        }
        image = new Blob(stbf.toString().getBytes());
        reader.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

成功创建并部署到 AppEngine 的端点。当我尝试通过 google-api-explorer https://developers.google.com/apis-explorer/?base=https://my-application.appspot.com/_ah/api#s/将简单对象插入数据存储区时,仅使用 id 和 appID 参数链接到图像和密钥,我收到以下错误:

503 Service Unavailable

- Show headers -

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "backendError",
    "message": "java.lang.NullPointerException"
   }
  ],
  "code": 503,
  "message": "java.lang.NullPointerException"
 }
}

当我将键从 Long 类型更改为 by 时,查询正确执行,并且我在数据存储区中看到了新实体。

除此之外,在文档中说“如果编码的键字段为空,则在保存对象时该字段将填充系统生成的键”。但似乎没有钥匙就不能接受?

有人可以帮我解决这个问题吗?

4

2 回答 2

6

看看生成的方法 containsImage:

标准生成的代码不检查 getKey() 是否返回 null。您需要包括以下内容:

if (image.getKey() == null) {
            return false;
}

在它执行 getObjectById 之前。

于 2013-03-25T22:42:05.697 回答
0

我一直在尝试解决这个问题。建议的答案是正确的。我想补充一点:这在 GAE SDK 或 Eclipse 插件中没有固定。即仍然没有检查空键。谢谢你的提示!抱歉,我无法添加评论,不知道为什么。

于 2013-04-30T06:29:47.757 回答