使用Java的Google App Engine中的多对多关系 [英] Many-To-Many relationship in Google App Engine using Java

查看:106
本文介绍了使用Java的Google App Engine中的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Google App Engine中的对象之间建立多对多关系?

我是GAE的新手,但仍在阅读它。编码看起来与我习惯的通常的Java编码大不相同。我已阅读入门留言板教程。那么,我可以从GAE用户处获得任何帮助/教程/视频/知识吗?

谢谢。

解决方案

关于文档,这是一个很好的开始:

< a href =http://code.google.com/appengine/docs/java/overview.html =nofollow> http://code.google.com/appengine/docs/java/overview.html



尊重 http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html


我们可以通过在关系的两边维护
键的集合来建立多对多关系。让我们将我们的示例调整为
让Food跟踪认为它是最喜欢的人:

Person.java p>

  import java.util.Set; 
import com.google.appengine.api.datastore.Key;

// ...
@Persistent
私人设置< Key> favoriteFoods;

Food.java

  import java.util.Set; 
import com.google.appengine.api.datastore.Key;

// ...
@Persistent
私人设置< Key> foodFans;




在这个例子中,Person维护一组键值
唯一标识了最喜欢的Food对象,而Food
维护一组Key值,用于唯一标识认为是最喜欢的Person
对象。使用键值对多对多
进行建模时,请注意,应用程序有责任维护关系的双方:

Album.java

  // ... 
public void addFavoriteFood(Food食物){
favoriteFoods.add(food.getKey());
food.getFoodFans()。add(getKey());
}

public void removeFavoriteFood(food food){
favoriteFoods.remove(food.getKey());
food.getFoodFans()。remove(getKey());
}


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.

解决方案

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());
}

这篇关于使用Java的Google App Engine中的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆