我有 Entity 对象,我想获取该对象。 目前,我正在使用
PersistenceManager.getObjectById(Object.class, Entity.getId());
问题在于,当实体中的所有属性都已可用时,这是对数据存储的额外调用。
最佳答案
这不是我认为您正在寻找的 Elixir ;它需要一些繁重的工作才能完成。
DAO代码
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
List<Foo> results = new ArrayList<Foo>();
Query query = new Query("Foo");
List<Entity> entities = datastore.prepare(query).asList(
FetchOptions.Builder.withDefaults());
for (Entity entity : entities) {
results.add(new Foo(entity));
}
Foo 类
public Foo(Entity entity) {
// TODO get properties from entity and populate POJO
this.bar=entity.getProperty("bar");
//get the key
//if the @PrimaryKey is a Long use this
this.id=entity.getKey().getId();
//if the @PrimaryKey is a String use this
this.id=entity.getKey().getName();
}
关于java - 在 App Engine 上将实体转换为它的类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2249328/