Google datastore - querying on key values

纵饮孤独 提交于 2019-12-22 05:07:35

问题


I have a EntityKind SuggestedInterest. When I populate that with a key "GrpId" and property "suggestedint".

Now, I need the "suggestedint" value for a requested "GrpId"

So, I write the query as:

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();       
}

When I execute this code I get

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

The developer docs told to use Entity.KEY_RESERVED_PROPERTY to query on keys, but I guess I misunderstood. What is the correct way to query on key ?


回答1:


You should pass it a Key instead of String:

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

then use it:

 Filter filter = 
    new FilterPredicate(Entity.KEY_RESERVED_PROPERTY,FilterOperator.EQUAL,grpKey);


来源:https://stackoverflow.com/questions/13673180/google-datastore-querying-on-key-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!