GAE: Querying on Child objects using parent Key

This post is about the Google App Engine and its limitations. I googled for a while to try and find an article on how to do better queries using the datastore, but I did not find what I needed. I hope this helps some of you out there.

I have a a bunch of child entities, but I only want a select few of them, so I need to query like good old SQL.

here is the scenario:

@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class A {

public class B  extends A {

@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class C extends A {
    B owner;

public class D extends C {
    boolean active;

public class E extends C {
    String otherProp;

As you can see A is my base object with some properties that I want all my objects to have. Now i want to get all Cs that are active and belong to a particular B. Well, the short answer is you cannot and that is because the relationship is not directly on the child class but is on its super class. This must be a bug!!!

Solution is to move the property down to both D and E, then I can do the query I want and get on with the rest of my life.


Key ownerKey = KeyFactory.stringToKey(ownerKeyString); 
String queryStr = "select from " + C.class.getName() + 
                        " where owner == ownerParam && active == true"; 
Query q = pm.newQuery(queryStr); 
q.declareVariables(B.class.getName() + " ownerParam"); 
myCs = (List<C>)q.execute(ownerKey); 

Here is a thread where I was discovering this.





Leave a Reply

Your email address will not be published. Required fields are marked *