0

I just wanna know how made a many to many relationship between two entity´s using the low level api in the datastore, I've been looking but I could not find documentation that explains how to do this kind of relationship. i hope some one can help.

but if I have the entity A and entity B in a many to many relationship how can i stored in the datastore. i mean, This a good solution to store a many to many realtionship in the datastore or what is a good solution? this code is ok or is wrong?

Entity entityA = new Entity("TypeA");
entityA.setProperty("name", "nameUserA");

Entity entityB = new Entity("TypeA");
entityA.setProperty("name", "nameUserB");

ds.put(entityA);
ds.put(entityB);

Entity entityChild = new Entity("entityChild",entityAKey);
entityChild.setProperty("name","child");

 ds.put(entityChild);

Entity entityChild = new Entity("entityChild",entityBKey);
entityChild.setProperty("name","child");

 ds.put(entityChild);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Alexander
  • 277
  • 1
  • 6
  • 17
  • What for entityChild is created? How you want to relations relations, between what entities? – Igor Artamonov Jul 21 '11 at 19:41
  • I'm just trying to represent a many to many ralatioship if I have one child entity and create this twice with the keys of each parent is a good solution? or is it better to create it only once and create an entity that contains the keys of these entities as properties? – Alexander Jul 21 '11 at 19:46
  • isn't it 1-1 (max 1-n, if there will be few children for one entity) relation? entityA - first entityChild, entityB - second entityChild – Igor Artamonov Jul 21 '11 at 19:49
  • What way you would keep a many to many relationship in the datastore using the low level api? – Alexander Jul 21 '11 at 19:54
  • If there is not big amount (few hundreds-thousands) of related entities, then i'll use list of keys of related items. Actually it's one-to-many relation, but it can be also queried by reverse relation, so it becomes many-to-many for most cases. – Igor Artamonov Jul 21 '11 at 20:00
  • but how you represent that in the datastore, i know that you can have a list of keys like parameter in your class, but in the datastore? you made a third entity to strore the keys of the entities in the relationship? – Alexander Jul 21 '11 at 20:08
  • You can save a list as a property for entity. as I know it've some litations, btw, like 5000 elements in list. this property can have index, can be filtered, etc. – Igor Artamonov Jul 22 '11 at 05:16

1 Answers1

0

AppEngine doesn't provide any functionality for many-to-many relationship.

You can emulate it, by creating your own entity, with two fields:

class AtoBRelations {
  Key entityA 
  Key entityB
}

I's common pattern to have an extra table for many-to-many relationship, even for other databases

BTW, for most cases it will be not optimal solution. Optimal solution depends on how this many-to-many relation have to be used, and there is few different solutions

for example there is second solution: you can store a list of related entities, like:

class EntityA {
   List<Key> entitiesB
}
Igor Artamonov
  • 35,450
  • 10
  • 82
  • 113
  • but if I have the entity A and entity B in a many to many relationship how can i stored in the datastore. i mean. – Alexander Jul 21 '11 at 19:22
  • you have to create an third entry `AtoBRelationship`, and store it also. btw, it better to tell what exactly you want to reach, and i tell you best solution for your exact scenario – Igor Artamonov Jul 21 '11 at 19:27
  • i already edit my question with more information , you say that i have to create a new entity that have two properties, the keys of the entities found in the relationship many to many? – Alexander Jul 21 '11 at 19:36
  • is a good way made a new entity with the keys of the relationhip? some thing like this Entity entityRelation = new Entity("relationA-B"); entityRelation.setProperty("keyA",KeyA);entityRelation.setProperty("keyB",KeyB); ? – Alexander Jul 21 '11 at 19:58