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:
Employee entities of John & Robert along with the respective Address
entities.
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 ?