获得客观实体的关键

时间:2011-08-23 13:56:59

标签: google-app-engine key objectify

虚拟问题。

我创建了我的POJO Objectify实体(例如,“类别”)并坚持下去。

然后我通过查询检索它。

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

我将在“产品”的代码中添加此代码:Key<Categoria> categoria;

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

3 个答案:

答案 0 :(得分:13)

用于物化4:

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

或者实体是否有@Parent

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

答案 1 :(得分:10)

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

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

并在需要的地方使用它:

anCategoria.getKey()

答案 2 :(得分:1)

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

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

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

product.categoria = Key.create(category);
相关问题