Filter Query definition by key field

I have the following Objectify relation:

@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 authorId(id, not entity), and I need to request a book for this author. My query is lower, but it returns an empty list, whereas I know that this author has books in the data warehouse. So how can I fix this request?

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();
  }
+4
source share
2 answers

You request a field authorIdinstead of the created one authorKey.

OfyService.ofy().load().type(Book.class).filter("authorKey", authorKey).order("-date").list();

With this change and the in-place index, you should get the result.

+1
source

@Index Book.authorKey Book.date. , multiproperty {authorKey, date}, , .

"" "authorKey".

+3

Source: https://habr.com/ru/post/1611858/


All Articles