Reputation: 27
I'm using objectify and the google cloud datastore.
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 | m
I want data of {1 ,2 ,3} not {1 ,2 ,3, m }
Do you know a way to have a more accurate result ? (without grandchildren)
Upvotes: 1
Views: 1369
Reputation: 13556
There is no elegant solution to this, and I suspect you are abusing @Parent
. It should be very, very rare to want to create grand@Parent relationships in the datastore. 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. 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.
Upvotes: 2
Reputation: 2012
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.
Upvotes: 0