我需要对这种实体执行 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/