问题
Is it possible to establish a Many-To-Many relationship between objects in Google App Engine?
I am a newbie in GAE and still reading about it. The coding seems quite different from the usual Java coding I am used to. I've read the Getting Started guestbook tutorial. So, can I get any help/tutorials/videos/knowledge from GAE users??
Thank you.
回答1:
About documentation this is a good start point:
http://code.google.com/appengine/docs/java/overview.html
Respect to many to many relationship from http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html :
We can model a many-to-many relationship by maintaining collections of keys on both sides of the relationship. Let's adjust our example to let Food keep track of the people that consider it a 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, the Person maintains a Set of Key values that uniquely identify the Food objects that are favorites, and the Food maintains a Set of Key values that uniquely identify the Person objects that consider it a favorite. When modeling a many-to-many using Key values, be aware that it is the app's responsibility to maintain both sides of the relationship:
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());
}
来源:https://stackoverflow.com/questions/7802196/many-to-many-relationship-in-google-app-engine-using-java