0

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;
}
David Underhill
  • 15,896
  • 7
  • 53
  • 61
KevMo
  • 5,590
  • 13
  • 57
  • 70
  • 1
    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 tags instead of funneling it through a Tag type. – bdonlan Jul 17 '09 at 19:05
  • That would be ideal, however there is more to my tag object than just the string. – KevMo Jul 17 '09 at 19:11

2 Answers2

3
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);
Peter Recore
  • 14,037
  • 4
  • 42
  • 62
0

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.

Maksim
  • 16,635
  • 27
  • 94
  • 135