如何从 Key<Entity> 中获取值?

How to get the value from a Key<Entity>?

我有这个:Key<Question> questionKey。现在我正试图得到这个问题。我一直在寻找一种方法,例如:questionKey.getValue() 但没有。

是使用ofy().load()的唯一方法吗?

您必须从数据库加载它。请参阅下面的示例。

Question question = ofy.load.key(questionKey).now();

或者您可以使用 Ref<T> 而不是 Key,这样您就可以直接访问实际的实体对象。

@Entity
class Car {
    @Id Long id;
    Ref<Person> driver;    // Person is an @Entity
}

Car car = new Car();
car.driver = Ref.create(driverKey);
ofy().save().entity(car).now();

Car fetched = ofy().load().entity(car).now();
Person driver = fetched.driver.get();

您可以找到更多详细信息 here