Robin Green
Robin Green

Reputation: 33033

Filter by key in App Engine datastore in Java

You would think that this would be an easy question to answer.

How do I, in Java, filter by an entity's key (not a property that just happens to be of type Key, but its actual key - what we would call the "primary key" in relational database land)?

I don't want to get a single entity with a particular key. I actually do want to do a filter and return a subset of the entities.

Upvotes: 4

Views: 1726

Answers (1)

Peter Knego
Peter Knego

Reputation: 80330

The trick is to use Entity.KEY_RESERVED_PROPERTY in place of property name:

Query q = new Query("MyEntity");
q.setFilter(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, 
                Query.FilterOperator.GREATER_THAN,
                KeyFactory.createKey("MyEntity", "somevalue")));

This will find all MyEntity entities with key greater than somevalue.

Upvotes: 8

Related Questions