anu anu - 4 months ago 23
Java Question

Google datastore - querying on key values

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 ?


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