[英]How to fetch nested child entities using parent entity key using datastore api
Here I'm using Datastore API to create, update & retrieve entities. 在这里,我正在使用Datastore API创建,更新和检索实体。
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. 在这里,我有Company
实体密钥,我需要获取Employee
详细信息以及配置给该员工的所有Address
实体。
Example: 例:
Company --> Aaa
Employee --> John
Address --> California
Address --> Redwood
Employee --> Robert
Address --> New York
Address --> Washington
My query will be as follows: 我的查询如下:
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query query = new Query("Employee").setAncestor(companyKey);
datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
My required result should be Employee entities of John & Robert along with the respective Address
entities. 我要求的结果应该是John&Robert的Employee实体以及相应的Address
实体。
Appengine documentation says: Appengine文档说:
public Query setAncestor(Key ancestor) 公共查询setAncestor(关键祖先)
" Sets an ancestor for this query. This restricts the query to only return result entities that are descended from a given entity. In other words, all of the results will have the ancestor as their parent, or parent's parent , or etc. If null is specified, unsets any previously-set ancestor. Passing null as a parameter does not query for entities without ancestors (this type of query is not currently supported) ." “ 设置此查询的祖先。这将查询限制为仅返回从给定实体派生的结果实体。换句话说,所有结果都将祖先作为其父代或父代的父代 ,等等。如果为null指定后,将取消设置任何先前设置的祖先。将null用作参数将不会查询没有祖先的实体(当前不支持此类型的查询) 。”
So your code should work fine. 因此,您的代码应该可以正常工作。 It will return all the entities with Aaa as the ancestor ie; 它将返回所有以Aaa为祖先的实体,即; both Employee (parent) and Address (parent's parent) entities. 雇员(父母)实体和地址(父母的父母)实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.