【问题标题】:Google App Engine - Recommended approach for querying by IdsGoogle App Engine - 按 ID 查询的推荐方法
【发布时间】:2013-10-31 14:59:10
【问题描述】:

任何人都可以推荐更好的方法来通过 GAE HRD 数据存储中的多个 id 查询实体?

1.

mgr = getEntityManager();           
Query dbQuery = mgr.createQuery("SELECT FROM CustomEntity as CustomEntity WHERE id IN (:ids)");
dbQuery.setParameter("ids", results.getIds());
return (List<CustomEntity>) dbQuery.getResultList();

2.

List<Key> customEntityKeys = new ArrayList<Key>();
for (String id : results.getIds()) {
    customEntityKeys.add(KeyFactory.createKey("CustomEntity", id));
}

mgr = getEntityManager();
JPADatastoreBridge jpaBridge = new JPADatastoreBridge();
List<CustomEntity> customEntities = new ArrayList<CustomEntity>();

Map<Key,Entity> customEntityMap = datastore.get(customEntityKeys);
for (Entity customEntityEntity : customEntityMap.values()) {
    customEntities.add((CustomEntity) jpaBridge.getJPAFromEntity(customEntityEntity, mgr, CustomEntity.class));                 
}
return customEntities;

在“更好的方法”中,我主要指的是性能方面。另外,如果有其他方法,我会很高兴听到的。 谢谢。

附言
我使用 JPA 作为我的持久性方法。不确定这是否真的很重要。

【问题讨论】:

    标签: google-app-engine jpa google-cloud-datastore


    【解决方案1】:

    我不知道大部分代码,但如果您问是否应该使用Query("SELECT.... WHERE ID IN....")datastore.get(...),第二个要好得多。 Gets 比使用 App Engine 数据存储区的查询效率高得多 - 更不用说它们始终是高度一致的,而非祖先查询仅是最终一致的。

    【讨论】:

      猜你喜欢
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多