我在objectify中有一个实体组,典型的SomeParentClass和SomeChildClass。我想做这样的事情从数据存储区加载SomeChildClass的实例。
ofy().load.type(SomeChildClass.class).id(idOfSomeChildClassInstace);
这没有找到任何结果。似乎您需要知道SomeChildClass的父级才能从datestore获取它。我知道这很有用。
Key<SomeChildClass> k = Key.create(someParentClass.generateKey(), SomeChildClass.class, idOfSomeChildClassInstace);
ofy().load().key(k).now;
如果我想在不知道父级的情况下加载SomeChildClass的实例,只需具有SomeChildClass的id。
答案 0 :(得分:16)
你不能这样做 - 实体的实际完整标识符是它的每个祖先的种类和id以及它自己的种类和id。这就是为什么构建完整的密钥有效,但仅使用子实体id不起作用。另一种看待它的方式只是在同一父母的兄弟姐妹之间是唯一的。
解决问题的最简单方法是为您的子实体生成一个密钥,然后为其获取“Web安全字符串”。该字符串包含实体及其所有父母的所有信息,可用于完全重建完整ID。
使用objectify:
String websafeKey = Key.create(parentKey, Entity.class, id).getString();
Key<Entity> key = Key.create(websafeKey);
如果需要,您也可以使用低级API执行此操作。
答案 1 :(得分:2)
您需要知道整个Key
能够get()
一个实体。子键包含:kind,ID 和父键。所以你需要提供这三个。