简体   繁体   English

对象化-给定父母的所有孩子

[英]Objectify - all children of a given Parent

I'm using objectify and the google cloud datastore. 我正在使用objectify和Google Cloud数据存储。

I'm looking for a way to get all children (without a specific type or kind) of a parent. 我正在寻找一种方法来让所有孩子(没有特定的类型或种类)成为父母。

I know the ancestor function. 我知道祖先的功能。

I used this 我用这个

ofy().load().ancestor(entity).list();

this give all children and grandchildren (i don't want it). 这给所有的孩子和孙子们(我不想要)。

In this exemple A is the parent of 1, 2 and 3. 1 is the parent of m 在这个例子中,A是1、2和3的父代。1是m的父代

A
 / | \
1  2  3
|     
m

I want data of {1 ,2 ,3} not {1 ,2 ,3, m } 我想要{1,2,3}而不是{1,2,3,m}的数据

Do you know a way to have a more accurate result ? 您知道获得更准确结果的方法吗? (without grandchildren) (没有孙子)

There is no elegant solution to this, and I suspect you are abusing @Parent . 没有@Parent解决方案,我怀疑您在滥用@Parent It should be very, very rare to want to create grand@Parent relationships in the datastore. 想要在数据存储区中创建grand @ Parent关系应该非常非常少。 Most hierarchical relationships in the datastore are best represented with traditional foreign key relationships. 数据存储中的大多数分层关系最好用传统的外键关系表示。

But if you are determined, one solution is to make 1, 2, and 3 all polymorphic objects of the same common kind. 但是,如果确定,一种解决方案是使1、2和3成为同一共同种类的所有多态对象。 So then you can query like this: 因此,您可以像这样查询:

ofy().load().type(BaseType.class).ancestor(parent)

Another advantage of having a type to query by is that you can now apply filters (kindless queries can't have filters): 使用查询类型的另一个优点是,您现在可以应用过滤器(无类型查询不能具有过滤器):

ofy().load().type(BaseType.class).ancestor(parent).filter("arbitraryField", value)

Another option, if the values you wish to exclude are rare, is to just fetch them anyways and filter them out. 如果您希望排除的值很少,另一种选择是无论如何都要获取它们并过滤掉它们。

I think the only way is to create an index and use filter on the parent property. 我认为唯一的方法是创建索引并在父属性上使用过滤器 The @parent will add the parent key as part of child key so by nature you will get all the descends and there is no simple way other than the type to filter. @parent会将父键添加为子键的一部分,因此从本质上讲,您将获得所有后代,除了过滤类型之外,没有其他简单的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM