I have the id of an child, and would like to get the parent Entity without looping through all entities.
I can not create the Key with KeyFactory.createKey, because I don't know the parent, and I can not use a filter on the Id, as I can not create a Key.
An example might clarify:
- Entity Supplier
- Entity Product, which has a Supplier as Parent.
How do I find the Supplier if I know the id (Key.getId()) of a product? What am I missing?
You can't, the
id by itself is not enough information because that's relative to its parent; take for example the following keys:
Key('Supplier', 1, 'Product', 1) Key('Supplier', 2, 'Product', 1) Key('Supplier', 1, 'Product', 2)
These are three unique products, but as you can see, only knowing that you're looking for
Product.id == 1 is not nearly enought to find the right one.
This situation doesn't really have a good solution, besides what you already figured (and ruled out) about looping through them all, and even then there might be multiple suppliers (as proven by my conveniently crafted example).
So you'll need a better design :) always keep in mind that in a parent/child relationship, the only valid key is a full key.