![](/img/trans.png)
[英]How to fetch only the specific child entity along with its Parent entities with specific value using Spring JPA DATA
[英]How to fetch nested child entities using parent entity key using datastore api
在这里,我正在使用Datastore API创建,更新和检索实体。
我的用例是,必须获取父实体的子实体和孙子实体。
我的实体结构如下:
Company
--> Employee
--> Address (Multiple Address entities).
在这里,我有Company
实体密钥,我需要获取Employee
详细信息以及配置给该员工的所有Address
实体。
例:
Company --> Aaa
Employee --> John
Address --> California
Address --> Redwood
Employee --> Robert
Address --> New York
Address --> Washington
我的查询如下:
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query query = new Query("Employee").setAncestor(companyKey);
datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
我要求的结果应该是John&Robert的Employee实体以及相应的Address
实体。
Appengine文档说:
公共查询setAncestor(关键祖先)
“ 设置此查询的祖先。这将查询限制为仅返回从给定实体派生的结果实体。换句话说,所有结果都将祖先作为其父代或父代的父代 ,等等。如果为null指定后,将取消设置任何先前设置的祖先。将null用作参数将不会查询没有祖先的实体(当前不支持此类型的查询) 。”
因此,您的代码应该可以正常工作。 它将返回所有以Aaa为祖先的实体,即; 雇员(父母)实体和地址(父母的父母)实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.