问题描述
我需要对这种实体进行getKey()
I need to do getKey()
with this kind of Entity:
@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()
函数时,出现此错误:
However the pattern I used before with version 3 seems to be not applicable anymore.The @Transient
is replaced by @Ignore
but when I annotate my getKey()
function with @Ignore
I get this error:
The annotation `@Ignore` is disallowed for this location
所以我只是注释掉了.看看是否能正常工作.
So I just commented it out. And see if it will work.
此外,
运行我的应用程序时,getKey()
函数会如上所述抛出NullPointerException
.
When I run my application the getKey()
function throws NullPointerException
as commented above.
那么,获取@Entity
键的方式是什么?
So, what is the pattern to get a @Entity
key?
推荐答案
您不能创建具有null或0 id的Key. Objectify和数据存储区都不允许.
You can't create a Key with a null or 0 id. Neither Objectify nor the datastore will allow it.
如果要从实体创建密钥,请确保首先具有有效的ID.
If you want to create a Key from an entity, make sure it has a valid id first.
这篇关于使用Objectify 4获取实体密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!