Get an Objectify Entity's Key

 ̄綄美尐妖づ 提交于 2019-12-01 04:07:54

I'm usually adding an extra method:

@Transient
Key<Categoria> getKey() {
   return Key.create(Categoria.class, id);
}

and use it where it needed:

anCategoria.getKey()

For objectify 4 use:

public Key<Foo> getKey() {
   return Key.create(Foo.class, id);
}

Or if the entity has a @Parent

public Key<Bar> getKey() {
   return Key.create(parentKey, Bar.class, id);
}

The Key class in Objectify 4 has this method:

public static <T> Key<T> create(T pojo)

so, if you already have read the entity (called category in this example) from the datastore, you can just call

product.categoria = Key.create(category);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!