Google App Engine projection query returns 0 results

纵饮孤独 提交于 2019-12-22 09:55:53

问题


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

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