JDO Relationships - App Engine
I'd like to implement a simple category system for my recipes.
Here's my Recipe entity:
@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;
}
...
}
And m开发者_StackOverflowy Category entity:
@PersistenceCapable
public class Category {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String name;
public Category(String name) {
this.name = name;
}
...
}
Now as you can see the association is simple. When I create a Recipe
I make sure I construct it with a list of categories. This is great. On my site I can just iterate over a Recipe
list (or whatever) and then just do .getCategories()
on it to retrieve the categories I need.
However, say I want to retrieve all categories in the datastore and when I click on one category I want to be able to retrieve all recipes for that category. What's the easiest to go about this?
To show all categories I have: select from Category.class group by name
.
But how do I retrieve all the recipes under a given category? Is my design flawed for JDO?
SELECT FROM mydomain.Recipe WHERE category = :category
Don't know what is this "flawed for JDO" idea ... JDO simply provides transparent persistence and lets you design your classes as you want.
精彩评论