如何使用查询应用条件从gae数据存储区中检索数据

时间:2013-06-04 06:07:48

标签: google-cloud-datastore jdo

这是我的问题。我正在使用JDO,数据保存在数据存储区后端。现在我想用带条件的查询检索数据。例如,

public List<Query> getQueries(final EntityKind entityKind) throws RuntimeException  {
    System.out.println("In getQueries()!!!" + entityKind);
    if (entityKind == null) {
        logger.log(Level.SEVERE, "QueryService : getQueries() : entityKind is null.");
        throw new RuntimeException("QueryService : getQueries() : entityKind is null.");

    }
    final long entityId = entityKind.getId();
    PersistenceManager pm = PMF.getPersistenceManager();

    List<Query> queryList = (List<Query>) new TransactionTemplate(pm).execute(new TransactionCallback<List<Query>>() {
        public List<Query> doInTransaction(PersistenceManager pm) {

            javax.jdo.Query query = pm.newQuery(Query.class,"SELECT FROM Query WHERE entityKindId == entityId");    
            Collection<Query> c = pm.detachCopyAll((Collection<Query>) query.execute(entityId));
            return new ArrayList<Query>(c);
        }
    });

    return queryList;
}

但这不起作用。我也试过这个,

public List<Query> getQueries(final EntityKind entityKind) throws RuntimeException  {
System.out.println("In getQueries()!!!" + entityKind);
if (entityKind == null) {
    logger.log(Level.SEVERE, "QueryService : getQueries() : entityKind is null.");
    throw new RuntimeException("QueryService : getQueries() : entityKind is null.");

}
final long entityId = entityKind.getId();
PersistenceManager pm = PMF.getPersistenceManager();

List<Query> entityKindFields = (List<Query>) new TransactionTemplate(pm).execute(new TransactionCallback<List<Query>>() {
        public List<Query> doInTransaction(PersistenceManager pm) {
            javax.jdo.Query query = pm.newQuery(Query.class);
            query.setFilter("entityKindId == :entityId");
            Collection<Query> c = pm.detachCopyAll((Collection<Query>) query.execute(entityKindId));
            return new ArrayList<Query>(c);
        }
    });
return queryList;
}

这也行不通。请帮忙!

1 个答案:

答案 0 :(得分:0)

如果您想通过ID检索实体,请使用

pm.getObjectById(CLASS.class, ENTITY_ID);

如果您想使用GQL检索它,请尝试THIS