在 GoogleEngine (Java) 上,在 JDO 中,如何根据父对象的 id 查询子对象列表?

发布于 2024-10-19 19:04:54 字数 348 浏览 1 评论 0原文

我有两个持久的值对象:日历和事件。日历有一个由事件列表组成的属性,具有一对多关系。日历是事件的父级,如下所示。

@Persistent
@Element(dependent = "true")
private List<Event> events;

现在,我希望能够根据日历对象键,通过 JDO 查询检索与日历相对应的事件。我对这两个类都使用encodedKey。

我想在事件实体上运行查询,而不仅仅是检索整个日历对象,因为我希望能够仅检索一组事件,以用于分页目的。

我尝试以任何可能的方式执行此操作,但无法弄清楚如何通过父键查询。

任何帮助将不胜感激。

I have two value objects, Calendar and Event, that are persistent. Calendar has a property that consists in a list of events, with a one to many relationship. Calendar is a parent of Events as shown below.

@Persistent
@Element(dependent = "true")
private List<Event> events;

Now, I would like to be able to retrieve via JDO query, Events corresponding to a Calendar, based on the Calendar object key. I am using encodedKey for both classes.

I want to run the query on the Event entity and not just retrieve the whole Calendar object, because I want to be able to retrieve only a set of events, for pagination purposes.

I tried to do this in any possible way, could not figure out how to query by parent key.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔日梦未散 2024-10-26 19:04:54

一些注意事项:

实体中的列表属性(例如您的List事件)存储为序列化的ProtocolBuffer。问题是:

  1. 如果此属性已建立索引,则它们的元素数将限制为 5000 个。

  2. 每次查询列表时,都需要反序列化整个列表。如果您可以有选择地检索列表元素,这就是问题的答案:您不能。

  3. 如果实体中有多个已编入索引的列表属性,则可能会导致索引爆炸

如果您想了解 GAE 数据存储的内部结构,那么这个视频是必须的:http://www.youtube .com/watch?v=AgaL6NGpkB8

解决方案:

  1. 使用 Slatkin 视频中的解决方案:使日历成为事件的父级(就数据存储而言)。然后添加父条件进行查询:Query.setAncestor(Key calendarKey)

    更新:实体父级关系应该用于创建“实体组”,例如事务范围所在的单元。

  2. 扭转情况:创建具有指向事件所属日历的日历属性的事件实体。然后,您可以简单地查询具有“calendar == calendarKey”的事件。

A few notes:

List properties in Entities (like your List<Event> events) are stored as a serialized ProtocolBuffer. The problem is:

  1. If this property is indexed, they are limited to 5000 elements.

  2. Every time you query the list, whole list needs to be deserialized. This is the answer to your question if you can selectively retrieve list elements: you can't.

  3. If you have multiple indexed list properties in entity then this can lead to Exploding Indexes.

If you want to understand internals of GAE datastore then this vido is a must: http://www.youtube.com/watch?v=AgaL6NGpkB8

Solutions:

  1. Use a solution from Slatkin's video: make Calendar a parent (in datastore terms) of Event. Then add a parent condition to query: Query.setAncestor(Key calendarKey).

    Update: Entity parent relationship should be used to create "entity groups", e.g. a unit on which transactions are scoped.

  2. Reverse the situation: Create Event entity that has Calendar property that points to Calendar to which event belongs. Then you can simply query Events that have 'calendar == calendarKey`.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文