7

哑问题。

我创建了我的 POJO Objectify 实体(例如,“Category”)并将其持久化。

然后我通过查询检索它。

我想以一对一的关系使用它,例如想将我的类别设置为一个或多个“产品”。

我将在我的“产品”代码中有这个:Key<Categoria> categoria;

所以问题是:如何找到检索到的实体的密钥以在我的产品中设置它?

4

3 回答 3

13

对于物化 4 使用:

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

或者如果实体有一个@Parent

public Key<Bar> getKey() {
   return Key.create(parentKey, Bar.class, id);
}
于 2013-02-06T12:37:19.517 回答
10

我通常会添加一个额外的方法:

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

并在需要的地方使用它:

anCategoria.getKey()
于 2011-08-23T15:12:15.417 回答
1

Objectify 4 中的 Key 类有这个方法:

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

因此,如果您已经category从数据存储中读取了实体(在此示例中调用),则可以调用

product.categoria = Key.create(category);
于 2013-10-11T14:34:28.873 回答