How to sort child entities using datastore api

0 views
Skip to first unread message

Awesome via StackOverflow

unread,
May 14, 2017, 2:23:05 AM5/14/17
to google-appengin...@googlegroups.com

Here I'm using Datastore API to create, update & retrieve entities.

My use case is, have to get the child and grand child entities of a parent entity.

My entity structure is as follows:

 Company
     --> Employee
            --> Address (Multiple Address entities).

Here I have Company entity key and I need to fetch Employee details along with all the Address entities configured to the employee.

Example:

Company --> Aaa
     Employee --> John 
                  Address --> California
                  Address --> Redwood
     Employee --> Robert
                  Address --> New York
                  Address --> Washington

My required result is:

  1. Employee entities of John & Robert along with the respective Address entities.

  2. Employee entities should be sort by name.

My query is as follows:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

Query query = new Query().setAncestor(companyKey);
datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());

Above query returning results specified in #1.

But not able to sort Employee entities by name using this query. When I tried to add sort clause query.addSort("name", SortDirection.ASCENDING) it is throwing an error as follows:

 java.lang.IllegalArgumentException: kind is required for all orders except __key__ ascending
at com.google.appengine.api.datastore.DatastoreApiHelper.translateError(DatastoreApiHelper.java:50)
at com.google.appengine.api.datastore.DatastoreApiHelper$1.convertException(DatastoreApiHelper.java:121)

How to sort child entities while using ancestor path in query ?



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/43960935/how-to-sort-child-entities-using-datastore-api
Reply all
Reply to author
Forward
0 new messages