Pergunta

I am apparently confused as to how the GAE ancestor query should work. My understanding was that an ancestor query should return all descendent generations, not just immediate children. Yet if I modify the example from the Developer Guide as follows, I retrieve only the immediate children, never the grandchild. What am I missing?

        Entity person = new Entity("Person", "tom");

        Entity weddingPhoto = new Entity("Photo", person.getKey());
        weddingPhoto.setProperty("imageUrl",
                                 "http://domain.com/some/path/to/wedding_photo.jpg");

        Entity babyPhoto = new Entity("Photo", person.getKey());
        babyPhoto.setProperty("imageUrl",
                              "http://domain.com/some/path/to/baby_photo.jpg");

// add this grandchild:

        Entity grandbabyPhoto = new Entity("Photo", babyPhoto.getKey());
        grandbabyPhoto.setProperty("imageUrl",
                              "http://domain.com/some/path/to/grandbabyPhoto.jpg");

        Entity dancePhoto = new Entity("Photo", person.getKey());
        dancePhoto.setProperty("imageUrl",
                               "http://domain.com/some/path/to/dance_photo.jpg");

        Entity campingPhoto = new Entity("Photo");
        campingPhoto.setProperty("imageUrl",
                                 "http://domain.com/some/path/to/camping_photo.jpg");
        getDatastore().put(
           java.util.Arrays.asList(person, weddingPhoto, babyPhoto, dancePhoto, campingPhoto));

        Query userPhotosQuery = new Query("Photo");
        userPhotosQuery.setAncestor(person.getKey());

        // This returns weddingPhoto, babyPhoto and dancePhoto, but
        // not grandbabyPhoto --- why???
        List<Entity> results = 
              getDatastore().prepare(userPhotosQuery).asList(
                 FetchOptions.Builder.withDefaults()); 

Thanks very much for any help you can provide!

Foi útil?

Solução

Because your put(..) statement does not contain grandbabyPhoto, so it does not get saved.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top