user3817287
user3817287

Reputation: 139

Objectify query doesn't work with ancestor

I have a ObjectS class that I store as an entity. In this ObjectS, I declare ObjectP as the parent in the following way.

@Parent
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private Key<ObjectP> objectPKey;

Then, my api looks like the following,

@ApiMethod(
        name = "getObjectPChildren",
        path = "getObjectPChildren",
        httpMethod = HttpMethod.POST
)
public Collection<ObjectS> getObjectPChildren(@Named("websafeObjectPKey") final String websafeObjectPKey)
         {

    Key<ObjectP> objectPKey = Key.create(websafeObjectPKey);
    Query<ObjectS> q = ofy().load().type(ObjectS.class)
            .ancestor(objectPKey);
    return q.list();
}

When I create the entity ObjectS (using some other API in the API explorer) with websafeObjectPKey, I can see the entities in the local datastore. However, the above query returns empty meaning that it says there are no entities :-(. Interestingly, if I modify the query to the following

Query<ObjectS> q = ofy().load().type(ObjectS.class);

it does return all the entities in the datastore! This means the datastore is somehow not aware of the ancestor relationship. I wonder what am I doing wrong?

Upvotes: 1

Views: 328

Answers (1)

user3817287
user3817287

Reputation: 139

If it helps someone else, I found that I while creating ObjectS (the constructor), I was not passing the right key of ObjectP. Fixed it and work fine now!

Upvotes: 1

Related Questions