user1938357
user1938357

Reputation: 1466

selecting few properties from entity in endpoint point class

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

Upvotes: 1

Views: 435

Answers (1)

tony m
tony m

Reputation: 4779

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.

Upvotes: 1

Related Questions