0

我只是想知道如何使用数据存储中的低级 api 在两个实体之间建立多对多关系,我一直在寻找,但我找不到解释如何建立这种关系的文档。我希望有人能帮帮忙。

但是如果我在多对多关系中拥有实体 A 和实体 B,我该如何存储在数据存储中。我的意思是,这是在数据存储中存储多对多关系的好解决方案,或者什么是好的解决方案?这段代码是好的还是错误的?

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);
4

1 回答 1

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
}
于 2011-07-21T19:12:25.267 回答