8

我有一个 EntityKind SuggestedInterest。当我用键“GrpId”和属性“suggestedint”填充它时。

现在,我需要请求的“GrpId”的“suggestedint”值

因此,我将查询编写为:

String findSuggestedInterest(String grpId)
{
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Filter filter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY,FilterOperator.EQUAL,grpId);
    Query q0 = new Query("SuggestedInterest").setFilter(filter);
    PreparedQuery pq0 = datastore.prepare(q0);
    Entity result = pq0.asSingleEntity();
    return result.getProperty("suggestedint").toString();       
}

当我执行此代码时,我得到

java.lang.IllegalArgumentException: __key__ filter value must be a Key

开发人员文档告诉使用 Entity.KEY_RESERVED_PROPERTY 来查询键,但我想我误解了。查询 key 的正确方法是什么?

4

1 回答 1

12

你应该通过它 aKey而不是String

Key grpKey = KeyFactory.createKey("SuggestedInterest", grpId)

然后使用它:

 Filter filter = 
    new FilterPredicate(Entity.KEY_RESERVED_PROPERTY,FilterOperator.EQUAL,grpKey);
于 2012-12-02T20:30:34.300 回答