javagcloud-java

Datastore create child entities with gcloud-java


How can I create an entity with a parent (which I created earlier)?

I didn't find anything in the docs which worked for me.

Please provide an example of code of how to create an entity with a parent.


Solution

  • Found it out by myself!

        Key key = datastore.newKeyFactory()
                .ancestors(PathElement.of("kind_of_parent", "id_of_parent"))
                .kind("kind_of_child")
                .newKey("id_of_child");
        Entity entity = Entity.builder(key)
                .set("x", 1)
                .set("y", 1)
                .set("z", 1).build();
    
        datastore.put(entity);