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