简体   繁体   English

如何使用数据存储区API使用父实体密钥获取嵌套的子实体

[英]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. 雇员(父母)实体和地址(父母的父母)实体。

Documentation Link 文档链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Spring JPA DATA 仅获取特定的子实体及其具有特定值的父实体 - How to fetch only the specific child entity along with its Parent entities with specific value using Spring JPA DATA 在 JPA 中使用父实体获取子实体 - Fetch a child entity using parent entity in JPA 如何使用join fetch通过一个查询获取父实体及其子实体及其子实体 - How to fetch Parent Entity with its Child Entities and their Child Entities with one query with join fetch 是否可以从父抽象实体中获取所有子实体? - Is it possible to fetch all child entities from a parent abstract entity? 如何使用java datastore.delete(key)从数据存储中删除实体 - How to delete an entity from a datastore, using java datastore.delete(key) 如何使用Pojo从数据存储中检索实体? - How to retrieve entities from datastore by using pojo? JPA 问题,使用父实体的主键作为子实体的主键 - JPA Problem, Using Parent Entity's Primary Key for Child's Entity's Primary Key 如何加入仅获取具有父实体的选定子实体 - How to join fetch only selected children entities with parent entity 如何从Google数据存储区获取最新实体? - How to fetch newest entities from Google Datastore? 如何在多对一关系中选择被 n 个父实体引用的子实体? - How to select a child entity that is referenced by n parent entities in a ManyToOne relationship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM