gpt4 book ai didi

java - 使用 Java 的 Google App Engine 中的多对多关系

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:33 25 4
gpt4 key购买 nike

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

最佳答案

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

http://code.google.com/appengine/docs/java/overview.html

尊重 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;

食物.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:

专辑.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 - 使用 Java 的 Google App Engine 中的多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7802196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com