AppEngine中的JDO:在从无主关系删除时保留原始实体 [英] JDO in AppEngine: keep the original entity when deleting from unowned relationship

查看:143
本文介绍了AppEngine中的JDO:在从无主关系删除时保留原始实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JDO 3.0语法定义了一个无主关系:

  @Persistent 
@未知数
私人设置<角色>角色;

我有几个应该分配给用户的预定义角色。我使用以下代码向用户添加/删除角色:

  roles.add(roleEntity); 
roles.remove(roleEntity);

问题是,像这样删除也会从数据存储中删除原始实体,但我只想删除参考资料。我知道我只能在父实体中存储能够解决问题的Keys,但有没有更好的解决方案?使用这种自然语法?

解决方案

我想这个问题应该得到一个真正的最终答案。

描述的行为是由JDO中的错误引起的,现在应该修复它。请参阅 http://code.google.com/p/datanucleus- appengine / issues / detail?id = 290 了解更多详情。另外请注意,无论文档如何描述,拥有的关系总是 dependent

不幸的是,在我移回到 JDO 2.0 后,它被修复了。在JDO 2.0中,我使用键集合或使用外键来实现 unowned code>。实际上这不是一个糟糕的解决方案,所以我不打算再次尝试 JDO 3.0 。对于密钥集合,我使用批处理获取来获取子对象,如果是外键,我使用的简单查询,其中声明。有时最好使用第一种解决方案,有时使用第二种解决方案。无论如何它都覆盖了一个干净的API,所以最终的代码很简单并且是自然的。 (我希望这种方法在文档中直接描述,所以我们不必很难学习它。)



重要的是,在这两种情况下,我都可以销毁关系而不删除原始实体。这是一个真正的无主关系。


I have defined an unowned relationship using JDO 3.0 syntax:

@Persistent
@Unowned
private Set<Role> roles;

I have several predefined roles which should be assigned to users. I use following code for adding/removing roles to users:

roles.add(roleEntity);
roles.remove(roleEntity); 

The problem is that removing like this also removes the original entity from the datastore but I just want to remove the reference. I know I could store only Keys inside the parent entity which would solve the problem, but is there a better solution? Using this "natural" syntax?

解决方案

I guess this question deserves a real final answer.

The described behavior was caused by a bug in JDO and it should be fixed by now. See http://code.google.com/p/datanucleus-appengine/issues/detail?id=290 for more details. Also notice, that owned relationships are always dependent, no matter what the documentation says.

Unfortunately, it was fixed after I had moved back to JDO 2.0. In JDO 2.0, I implement unowned relationships using collections of keys or using foreign keys. And actually it's not a bad solution so I don't plan to try JDO 3.0 again. For a collection of keys I use batch fetch to get child objects, in case of foreign keys, I use simple queries with where statement. Sometimes it's better to use the first solution, sometimes the second one. It's all covered by a clean API anyway, so the final code is simple and "natural". (I wish this approach was described directly in the documentation so we don't have to learn it the hard way.)

The important thing is that in both cases I can destroy the relationship without deleting original entities. It's a true unowned relationship.

这篇关于AppEngine中的JDO:在从无主关系删除时保留原始实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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