views:

17

answers:

1

Hi,

I have a data model where I'm not sure if I'll get any benefit from nesting classes in entity groups. The basic model is that User owns many horses, a horse can only own one saddle:

// Model 1, nest everything:
User
    Horse
        Saddle
    Horse
        Saddle

// Model 2: Just let them all be siblings.
User
    Horse
    Horse
    Saddle
    Saddle

I'm not sure if I gain any advantage over nesting saddle instances under a horse, or if I should just leave them all as siblings.

Generating keys:

// Model 1:
Key key = new KeyFactory.Builder(
  User.class.getSimpleName(), userid)
    .addChild(Horse.class.getSimpleName(), horseid)
      .addChild(Saddle.class.getSimpleName(), saddleid).getKey();

// Model 2:
Key key = new KeyFactory.Builder(
  User.class.getSimpleName(), userid)
    .addChild(Saddle.class.getSimpleName(), saddleid).getKey();

So generating keys is more work for situation 1, and I'm wondering if I should just switch to situation 2 instead, if I'm not gaining any advantage over the additional nesting? The operations I have to do:

  1. Delete a horse and its saddle.
  2. Create a horse with a saddle.
  3. Update a horse and its saddle.
  4. Get a horse by itself.
  5. Get a horse and its saddle at the same time.

Both models give me all the above, I think. Maybe model #1 offers the advantage that the entire User object doesn't have to be locked when performing operations only on a Saddle?

Thanks

+1  A: 

Per the docs:

Every entity with a given root entity as an ancestor is in the same entity group. All entities in a group are stored in the same datastore node.

And:

Only use entity groups when they are needed for transactions. For other relationships between entities, use ReferenceProperty properties and Key values, which can be used in queries.

Both of your models will result in one entity group per user, regardless of nesting. If you just want to represent a hierarchy of objects, you should avoid entity groups entirely and use a ReferenceProperty.

Drew Sears