google-app-engine - 使用 Objectify 4 获取实体键

标签 google-app-engine objectify

我需要对这种实体执行 getKey():

@Entity
public class Value {
    @Id
    private long id;
    private byte[] value;

    com.googlecode.objectify.Key<Value> getKey() {
        return com.googlecode.objectify.Key.create(Value.class, id); // When executed this line throws NullPointerException
    }

        // Code omitted
}

然而,我之前在版本 3 中使用的模式似乎不再适用。 @Transient 被替换为 @Ignore 但是当我用 @Ignore 注释我的 getKey() 函数时我得到这个错误:

The annotation `@Ignore` is disallowed for this location 

所以我就把它注释掉了。看看它是否会起作用。

此外,

当我运行我的应用程序时,getKey() 函数会抛出 NullPointerException,如上文所述。

那么,获取 @Entity 键的模式是什么?

最佳答案

您不能使用 null 或 0 id 创建 key 。 Objectify 和数据存储都不允许这样做。

如果要从实体创建 key ,请先确保它具有有效的 ID。

关于google-app-engine - 使用 Objectify 4 获取实体键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12830906/

相关文章:

python - [GAE]模板中如何设置 `inline`

javascript - 从 jquery-json 检索数据到 python

java - appengine 单元测试仅获取 10 个实体

java - Google Cloud - 数据存储与本地计算机相比太慢

google-app-engine - Django-nonrel ≤1.3 和 Google App Engine 上的 Memcache

node.js - Google App Engine 上的私有(private) Node 模块

javascript - 在 Google App Engine 中使用 JavaScript 将可下载文件添加到另一个页面

java - 对象化和 TimerTask : No API environment is registered for this thread

java - 使用 Objectify 将父项/子项批量存储到 Appengine 上

java - 并行更新不同的实体属性