Datastore create child entities with gcloud-java

75 Views Asked by At

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.

1

There are 1 best solutions below

0
Luis Kleinwort On BEST ANSWER

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);