如何从多对多关系中获取每个对象
人是一个有名字和最喜欢的食物的对象,
Food是一个有名字和foodFans的对象。
人与食物之间的关系是多对多
一个人可以有很多喜欢的食物,
而且食物可以有很多食物粉丝
我写了一个简单的java来获取一个人最喜欢的食物
我想知道我所做的是否正确?
有更好的解决方案吗?
谢谢
PersistenceManager pm = PMF.get().getPersistenceManager();
//select from ...
Query query = pm.newQuery(Person.class);
//where ...
query.setFilter("name == nameParam");
//order by ...
query.setOrdering("id desc");
//declare a parameter to use later
query.declareParameters("String nameParam");
try {
@SuppressWarnings("unchecked")
//execute query
List<Person> results = (List<Person>) query.execute(person_name);
//if found
if (results.iterator().hasNext()) {
for (Person p : results) {
out.println("<p>" + p.getKey() + "</p>");
out.println("<p>" + p.getName() + "</p>");
Set<Key> foods = p.getFavoriteFoods();
Iterator<Key> i = foods.iterator();
while(i.hasNext()) {
Food f = pm.getObjectById(Food.class, i.next());
out.println("<p>" + f.getName()+ "</p>");
i.remove();
}
}
} else {
out.println("<p>Not Found.</p>");
}
} finally {
query.closeAll();
}
Person is an object has name and favoriteFoods,
Food is an object has name and foodFans.
the relationship between Person and Food is many to many
A person can have lots of favoriteFoods,
and the food can have lots of foodFans
i wrote a simple java to get the each favoriteFoods from one person
i want to know what i have done is correct?
Is there a better solution?
THANKS
PersistenceManager pm = PMF.get().getPersistenceManager();
//select from ...
Query query = pm.newQuery(Person.class);
//where ...
query.setFilter("name == nameParam");
//order by ...
query.setOrdering("id desc");
//declare a parameter to use later
query.declareParameters("String nameParam");
try {
@SuppressWarnings("unchecked")
//execute query
List<Person> results = (List<Person>) query.execute(person_name);
//if found
if (results.iterator().hasNext()) {
for (Person p : results) {
out.println("<p>" + p.getKey() + "</p>");
out.println("<p>" + p.getName() + "</p>");
Set<Key> foods = p.getFavoriteFoods();
Iterator<Key> i = foods.iterator();
while(i.hasNext()) {
Food f = pm.getObjectById(Food.class, i.next());
out.println("<p>" + f.getName()+ "</p>");
i.remove();
}
}
} else {
out.println("<p>Not Found.</p>");
}
} finally {
query.closeAll();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来你正在使用 JDO。应用程序引擎 JDO 文档使用了这个确切的示例 - 人和最喜欢的食物 - 并且它们 描述一种推荐的实现方式:无主多对多关系。
具体来说,您首先要确定哪一方的相关实体通常较少。在这种情况下,平均食物的粉丝数(数百万)可能比普通人最喜欢的食物(数十到数百)还要多。
在 Person 中放置一个键列表属性,例如
Set; favoriteFoods
,用于他们最喜欢的食物。要获取一个人最喜欢的食物,您只需加载 Person 实体(这需要一次数据存储获取)并查看favoriteFood
集。要获取某个食物的粉丝,您需要查询favoriteFood
属性包含该食物(在 GQL 中为=
)的 Person 实体。looks like you're using JDO. the app engine JDO docs use this exact example - people and favorite foods - and they describe one recommended way to implement it: unowned many-to-many relationship.
specifically, you'd first determine which side will generally have fewer related entities. in this case, the average Food will probably have more fans (millions) than the average Person will have favorite foods (tens to hundreds).
put a key list property in Person, e.g.
Set<Key> favoriteFoods
, for their favorite foods. to get a person's favorite foods, you'd just load the Person entity (which costs one datastore get) and look at thefavoriteFood
set. to get a food's fans, you'd query for the Person entities whosefavoriteFood
property contains (=
in GQL) that food.