0

I have been banging my head against the wall trying to correctly configure a many to many relationship which should be straight forward.

I have A shopping list class, and an items class. I simply want shopping lists to be able to hold a list of items.

Please Please don't reply with the Google App engine documentation link, I'm looking for a working example that someone has used within their own project, not the theory Google loosely throws around.

jmac
  • 7,078
  • 2
  • 29
  • 57
NathofGod
  • 479
  • 5
  • 12

2 Answers2

0

You can't 'hold' items inside a parent item like you can in mongodb.

You can however reference your items class from your shopping list class

Take the IDs from your item class and add them to your shopping list class. It will look something like:

db.ListProperty(db.Key)

I know you dont want gae documentation but they have a page for modelling relationships here

andy boot
  • 11,355
  • 3
  • 53
  • 66
0

It's easy.

Create a class that holds the many-to-many relationship. In your case:

class ShoppingListItems(){

  @Id
  private String id;
  private Long shoppingListId;
  private Long itemId;

  public ShoppingListItems(Long shoppingListId, Long itemId) {
    this.id = shoppingListId.toString() + "-" + itemId.toString();
    this.shoppingListId = shoppingListId;
    this.itemId = itemId;
  }

  public String getId() {
    return id;
  }

  public static void addItemToList(Long shoppingListId, Long itemId, DAO dao) {
        // add shopping list and itemsr IDs to the ShoppingListItems 'join' table
        ShoppingListItems record = new ShoppingListItems(shoppingListId, itemId);
        dao.ofy().put(record);   
  }

Here @Id and dao.ofy().put(record) comes from Objectify lingo but you can use JPA or JDO as you prefer.

supercobra
  • 15,810
  • 9
  • 45
  • 51
  • This is what I was originally going to do but after reading Google documentation I was convinced there was a better way to do it as it suggested you could mimic actual relational databases. Thank you for your example – NathofGod May 14 '12 at 19:40