0
votes

I have a Course entity which contains a Set of Keys to my Tag entity. How would I go about creating a query to get a list of courses with a specific tag? For example, I want to find all the courses tagged with java.

Here are my entities:

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
 public class Course{

 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 private Key key;

 @Persistent private Set<Key>       tags;
 //etc
 }

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Tag{

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent private String tagText;
}
2
As an added note, with the google datastore, since you don't have joins, it's often a good idea to denormalize your data a bit. That is, have a Set<String> tags instead of funneling it through a Tag type. - bdonlan
That would be ideal, however there is more to my tag object than just the string. - KevMo

2 Answers

3
votes
Tag tag = getTagFromString("java");
Key tagKey = tag.getKey();  // i will assume you have a getKey() method

PersistenceManger pm = PMF.get().getPersistenceManager();
Query q = pm.newQuery(Course.class);
q.setFilter("tags == :tagParam");

List<Course> coursesTaggedWithJava = (List<Course>) q.execute(tagKey);
0
votes

I don't think this is possible. Google DataStore does not allow to use Join queries. But I may be mistaken.

Here and here is the website where you can find more information about GQL.