oviroa
oviroa

Reputation: 1099

On GoogleEngine (Java), in JDO, how do I query a list of child objects based on the id of the parent?

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.

Upvotes: 3

Views: 1037

Answers (1)

Peter Knego
Peter Knego

Reputation: 80340

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`.

Upvotes: 4

Related Questions