0

这是我的问题。因为我使用的是 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;
}

这也行不通。请帮忙!

4

1 回答 1

0

如果要按 ID 检索实体,请使用

pm.getObjectById(CLASS.class, ENTITY_ID);

如果你想使用 GQL 检索它,试试这个

于 2013-06-12T17:56:47.183 回答