问题
I am trying to perform a projection query in order to fetch several properties from each entity in my datastore of over ten thousand entities. I have read and followed the documentation, but my query is not returning any results. I've even simplified my projection down to only projecting a single property, the entity's ID field, but still get 0 results. Here is my simplified code:
Query q = new Query("MyEntity");
q.addProjection(new PropertyProjection("entityId", Long.class));
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
PreparedQuery pq = datastore.prepare(q);
int count = pq.countEntities(FetchOptions.Builder.withLimit(1000));
log.info("query contains " + count + " items.");
When I run this code, count
equals 0. If I simply remove the second line so that the query has no projections, count
equals 1000.
I am using Objectify in my app, but I am using the GAE low-level API for projection queries because I'm using Objectify v3, which doesn't support projection queries. It would be a lot of work to change my code to support Objectify v4.
The entityId
field I'm projecting looks like this in my Objectify entity object:
@Id Long entityId;
回答1:
So it turns out my simplified case of projecting only the ID property turned out to be the problem. When I tested the same code on any other property field, the projection query works.
When creating projection queries, the ID property should not be projected. It is still included in the resulting entity, but including it in the projections causes the query to turn up no results. The ID property is not stored the same way as the other property fields in the entity.
来源:https://stackoverflow.com/questions/15760437/google-app-engine-projection-query-returns-0-results