From many to many relationships in java google ap engine

how would I start creating many, many relationships between data objects in the Google engine (using jdo)

The application engine page talks about 1-lot and 1-1, but not many. Any sample code would be much appreciated

+4
source share
2 answers

As a rule, for many, many, you would make two 1-manys. And from the application with interaction documents :

Many-to-Many Relationships We can model many-to-many relationships by storing collections of keys on either side of the relationship. Let us customize our example to let Food keep track of people who consider this to be their favorite:

Person.java import java.util.Set; import com.google.appengine.api.datastore.Key; // ... @Persistent private Set<Key> favoriteFoods; Food.java import java.util.Set; import com.google.appengine.api.datastore.Key; // ... @Persistent private Set<Key> foodFans; 

In this example, a Person maintains a set of key values ​​that uniquely identify food items that are favorites, and Food supports a set of key values ​​that uniquely identify Person objects that consider him a favorite. When modeling many-to-many using key values, keep in mind that the responsibility for the content of both parties depends on the responsibility of the application: Album.java

 // ... public void addFavoriteFood(Food food) { favoriteFoods.add(food.getKey()); food.getFoodFans().add(getKey()); } public void removeFavoriteFood(Food food) { favoriteFoods.remove(food.getKey()); food.getFoodFans().remove(getKey()); } 

Please note that if the Person instance and the Food instance contained in Person.favoriteFoods are in the same group of individuals, it is not possible to update the person and this favorite food in one transaction. If it is not practical to place objects in the same group of entities, the application must take into account the possibility that the person’s favorite products will be updated without a corresponding update of the product set for fans, or, conversely, that the product set for fans will be updated without an appropriate update for the set of fans of favorite products .

+8
source

This page contains many-to-many information. This is not in TOC, but you can find it by looking for many-to-many.

We can model many-to-many relationships by storing collections of keys on either side of the relationship. Let us customize our example to let Food keep track of people who consider this to be their favorite:

Person.java

 import java.util.Set; import com.google.appengine.api.datastore.Key; // ... @Persistent private Set<Key> favoriteFoods; 

Food.java

 import java.util.Set; import com.google.appengine.api.datastore.Key; // ... @Persistent private Set<Key> foodFans; 
+3
source

All Articles