I have the following Objectify relationship:
@Entity(“Author”)
public class Author{
@Id
private long authorId;
…
}
@Entity(“Book”)
public class Book{
@Id
private long id;
private Key<Author> authorKey;
…
}
Now for the fun part: I have the authorId
(the id, not entity) and I need to query Book for that author. My query is below, but it is returning an empty list, whereas I know for a fact that this author has books in the datastore. So how might I fix this query?
public static List<Book> getBooksForAuthor(Long authorId) {
Key<Author> authorKey = Key.create(Author.class, authorId);
return OfyService.ofy().load().type(Book.class).filter("authorKey", authorKey).order(“-date").list();
}
You will need to @Index
the Book.authorKey
and Book.date
fields. I can't remember offhand if this will require a multiproperty index on {authorKey, date}, but try without and see if it works.
I would also recommend that you call the field 'author' not 'authorKey'.
You are querying with the field authorId
instead of the authorKey
that you just created.
OfyService.ofy().load().type(Book.class).filter("authorKey", authorKey).order(“-date").list();
With this change and the index in place, you should get the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With