如何从多对多关系中获取每个对象

发布于 2024-10-20 07:32:39 字数 1450 浏览 0 评论 0原文

人是一个有名字和最喜欢的食物的对象,
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ懵逼小萝莉 2024-10-27 07:32:39

看起来你正在使用 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 the favoriteFood set. to get a food's fans, you'd query for the Person entities whose favoriteFood property contains (= in GQL) that food.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文