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

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

问题描述

是否可以在 Google App Engine 中的对象之间建立多对多关系?
我是 GAE 的新手,仍在阅读有关它的信息.编码似乎与我习惯的通常的 Java 编码大不相同.我已经阅读了入门留言簿教程.那么,我可以从 GAE 用户那里获得任何帮助/教程/视频/知识吗??
谢谢你.

解决方案

关于文档,这是一个很好的起点:

http://code.google.com/appengine/docs/java/概览.html

尊重来自 http://code 的多对多关系.google.com/appengine/docs/java/datastore/jdo/relationships.html:

<块引用>

我们可以通过维护多对多关系的集合来建模关系双方的关键.让我们调整我们的例子让 Food 跟踪那些认为它最喜欢它的人:

Person.java

import java.util.Set;导入 com.google.appengine.api.datastore.Key;//...@执着的私有集<密钥>最喜欢的食物;

食品.java

import java.util.Set;导入 com.google.appengine.api.datastore.Key;//...@执着的私有集<密钥>美食爱好者;

<块引用>

在这个例子中,Person 维护了一组 Key 值唯一标识收藏夹的 Food 对象,以及 Food维护一组唯一标识人员的键值认为它是最喜欢的对象.建模多对多时使用键值,请注意应用程序有责任维护双方关系:

相册.java

//...public void addFavoriteFood(Food food) {最喜欢的Foods.add(food.getKey());food.getFoodFans().add(getKey());}public void removeFavoriteFood(Food food) {最喜欢的Foods.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天全站免登陆