1

I am trying to modify a standard select query in my endpoint class to fetch selected fields from my entity. However even after changing the query, I see that my query result is fetching all the fields. The code is below, you can note that the query has been updated to add my own. I am basically trying to form a query where I only fetch few properties from an entity rather than getting all(to reduce network data transaction volume). Any help is appreciated.

//QUERY MODIFIED IN THIS METHOD
/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listQuizTable")
public CollectionResponse<QuizTable> listQuizTable(
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

    EntityManager mgr = null;
    Cursor cursor = null;
    List<QuizTable> execute = null;

    try {
        mgr = getEntityManager();
        //Query query = mgr.createQuery("select from QuizTable as QuizTable");

        Query query = mgr.createQuery("select n.quizKey, n.quizDesc, n.uploadDate from QuizTable n");

        if (cursorString != null && cursorString != "") {
            cursor = Cursor.fromWebSafeString(cursorString);
            query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
        }

        if (limit != null) {
            query.setFirstResult(0);
            query.setMaxResults(limit);
        }

        execute = (List<QuizTable>) query.getResultList();
        cursor = JPACursorHelper.getCursor(execute);
        if (cursor != null)
            cursorString = cursor.toWebSafeString();

        // Tight loop for fetching all entities from datastore and accomodate
        // for lazy fetch.
        for (QuizTable obj : execute)
            ;
    } finally {
        mgr.close();
    }

    return CollectionResponse.<QuizTable> builder().setItems(execute)
            .setNextPageToken(cursorString).build();
}
user1938357
  • 1,466
  • 3
  • 20
  • 33
  • I wonder why has someone down voted this query. Google app engine and endpoints are still evolving as I write. There is not much documentation which google also has on this. If posting such queries on stackkoverflow causes downvoting then that's quite discouraging. I had been answering quite a lot of questions to help other users, based on what I have learned over past 1 month since I starting setting app engine backend. – user1938357 Jun 16 '13 at 08:55

1 Answers1

1

Projection queries for GAE datastore should serve your purpose. It will return only the required fields in query results and leave the unwanted fields blank. Now to receive this modified response through cloud endpoints, modify your response item representing an individual entity such that it contains only the required fields instead of all the fields in your entity. Then repeat this modified individual response item to create a collection response item.

Projection queries have some limitation on the kind of queries you can do, for example: a field required in the result cannot be used in an equality filter. If you hit such a limitation in your case, then you can use the 2nd option directly without using projection queries. That is, do a normal query and then use the modified individual and collection response items so that they send only the required fields through cloud endpoints.

tony m
  • 4,769
  • 1
  • 21
  • 28
  • that's very helpful, thanks for that. I should be able to find my way out now. Getting this work in the endpoint is the only challenge which I am trying out now, keep your suggestions also in mind. – user1938357 Jun 16 '13 at 12:01