title: Querying one-to-many model in Google appengine
body: <p>Suppose i've a one-to-many model that involves storing several books and their chapters like </p>
  
  <pre><code>@Entity
  public class Book {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private Key id;
  
      private String title;
  
      @OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
      private List&lt;Chapter&gt; chapters = new ArrayList&lt;Chapter&gt;();
  
      // getters and setters
  }
  
  
  @Entity
  public class Chapter {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private Key id;
  
      private String title;
      private int numPages;
  
      @ManyToOne(fetch = FetchType.LAZY)
      private Book book;
  
      // getters and setters
  }
  </code></pre>
  
  <p>and then persisting it like </p>
  
  <pre><code>Book b = new Book();
  b.setTitle("JDO 4eva");
  Chapter c1 = new Chapter();
  c1.setTitle("Intro");
  c1.setNumPages(10);
  b.getChapters().add(c1);
  Chapter c2 = new Chapter();
  c2.setTitle("Configuration");
  c2.setNumPages(9);
  b.getChapters().add(c2);
  
  pm.currentTransaction().begin();
  try {
      pm.makePersistent(b);
      pm.currentTransaction().commit();
  } finally {
      if (pm.currentTransaction().isActive()) {
          pm.currentTransaction().rollback();
      }
  }
  </code></pre>
  
  <p>My question however is how do i select only chapters of a specific book only since all the chapters are stores in the same entity kind but there's not reference("foreign key") of the corresponding book. </p>
  
  <p>In the normal sql i would for instance query it like "select * from chapters where book="book_id". What's the equivalent of this query in appengine? Thanks alot.</p>
  
  <p>NB: I got some of the code from <a href="http://gae-java-persistence.blogspot.com/2009/10/creating-bidrectional-owned-one-to-many.html" rel="nofollow">here</a> for illustration purposes. </p>