How to implement cascading deletion in Objectify?

I have the following hierarchy.

GrandParent β†’ Parent β†’ Child

Using the parent and child languages @Parent Ref<GrandParent> and @Parent Ref<Parent> to create the parent relationship.

I am trying with a good way to do a cascading delete for GrandParent .

Of course, I could download all the children, generate keys to them and delete them by key. It seems terribly inefficient. Is there something I could ask the parent and turn the query results into a list of keys without having to do a full selection?

Any thoughts or third-party libraries are welcome.

+7
google-cloud-datastore objectify
source share
2 answers

Basically, what Michael said, but here is the cleanest way I found for this.

 ofy().delete().keys(ofy().load().ancestor(entityKey).keys().list()); // ancestor included 

entityKey here is the key of the object you want to delete (in case this was not obvious)

  • it will handle any level of children, regardless of their types.
  • as a cheap call, since you will receive due to using only the key request keys()
+8
source share

The problem here is that Google Data Warehouse is not a relational database. it is a repository with a key, so it doesn’t so much really connect 3 objects, but includes links to each other. This means that there is no real way to cascade deletion.

Thus, it is best to ask the children, get their entities and then delete them one at a time (here you can find a good example)

+8
source share

All Articles