How do you implement cascading delete in Objectify?

我们两清 提交于 2019-12-06 01:29:54

问题


I have the following heriacy.

GrandParent --> Parent --> Child

Parent and Child use @Parent Ref<GrandParent> and @Parent Ref<Parent> to create there parent relationship.

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

I of course I could load all the children, generate keys from them and delete by key. This seems terribly inefficient. Is there something where I could query by parent and turn the query results into a list of keys without having to do the full fetch?

Any thoughts, or third party libraries welcome.


回答1:


Basically, what Michael said, but here is the cleanest way I have found to do it.

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

entityKey here is the key of the entity you want to delete (just in case that wasn't obvious)

  • this will handle any level of children, no matter their types.
  • as cheap of a call as you are going to get due to the use of a key only query keys()



回答2:


The issue here is that the Google Datastore is not really a relational database. it's a key-value store, so it's not so much truly connecting the 3 entities so much as just including references to each other. That means that there's no real way for a cascading delete.

As such, your best bet would be to query the children, fetch their entities, and then delete them one at a time (a good example can be found here)



来源:https://stackoverflow.com/questions/26455098/how-do-you-implement-cascading-delete-in-objectify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!