我想为我的食谱实现一个简单的类别系统。
这是我的食谱实体:
@PersistenceCapable
public class Recipe {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private List<Category> categories;
public Recipe(List<Category> categories) {
this.categories = categories;
}
...
}
我的分类实体:
@PersistenceCapable
public class Category {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String name;
public Category(String name) {
this.name = name;
}
...
}
现在您可以看到关联很简单。当我创建Recipe
时,我确保使用类别列表构建它。这很棒。在我的网站上,我可以迭代Recipe
列表(或其他),然后只需.getCategories()
就可以检索我需要的类别。
但是,我想要检索数据存储区中的所有类别,当我点击一个类别时,我希望能够检索该类别的所有配方。什么是最容易实现的?
显示我拥有的所有类别:select from Category.class group by name
。
但是如何检索给定类别下的所有食谱? JDO的设计是否有缺陷?
答案 0 :(得分:2)
SELECT FROM mydomain.Recipe WHERE category =:category
不知道这对“JDO有缺陷”的想法是什么...... JDO只是提供透明的持久性,让你可以根据需要设计你的课程。