Google App Engine Datastore 如何获取 Key<?> 的实体

Google App Engine Datastore how to get entity for Key<?>

我是 Datastore 的新手,现在我正在使用 Datastore 和 Objectify 开发 Gae 应用程序。实体 class 的形式为

@Entity
public class MyClass1 {
  public Key<MyClass2> field1;
  @Id Long id;
  and so on
 }

MyClass2 的形式为

@Entity
public class MyClass2 {
  ....
  @Id public Long id;
  @Index public String field2;
  ....
}

我有 MyClass1 实体。我怎样才能得到 field2 的价值? 如果我使用 DatastoreService1.get(myclass1.field1) 我得到

   method get(Key) in the DatstoreService is not applicable for arguments (Key<MyClass2>)

请帮帮我

你的错误与问题并没有真正的关系,所以我假设你想得到 field1 的值,而不是 field2.

错误是因为 DatastoreService get() 方法需要 com.google.appengine.api.datastore.Key, but you are passing it a com.googlecode.objectifyKey<T>, which is an Objectify key - they're not the same thing. Here are the docs.

我建议使用 Ref<MyClass2>,因为您可以这样做:

MyClass2 myClass2 = myClass1.field1.get();

(使用您的代码示例)。