你如何在Objectify中实现级联删除? [英] How do you implement cascading delete in Objectify?

查看:125
本文介绍了你如何在Objectify中实现级联删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下不幸。



GrandParent - > Parent - > Child



父母和子女使用 @ Parent Ref< GrandParent> @Parent Ref< Parent> 创建双亲关系。

我试图用一种很好的方式为 GrandParent 做一个级联删除。



I当然我可以加载所有的孩子,从他们那里生成密钥并通过密钥删除。这似乎非常低效。有什么我可以通过父母查询,并将查询结果变成一个键列表,而不必完整的获取?



任何想法或第三方库欢迎。

解决方案

基本上,迈克尔说过,但这里是我找到的最干净的方式。 b)
$ b

  ofy()。delete()。keys(ofy()。load()。ancestor(entityKey).keys()。list()) ; //祖先包含

entityKey 这里是你想要的实体的关键删除(只是在以下情况下并不明显)


  • 这将处理任何级别的子级,无论它们的类型如何。
  • >
  • 因为使用了一个只有一个键的查询,所以您将获得一个低成本的调用 keys()


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.

解决方案

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

这篇关于你如何在Objectify中实现级联删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆