title: Hibernate select one to many help
body: <p>I have the following classes mapped as one to many: Reader and book when one reader can hold more then one book:</p>
  
  <p>Book:</p>
  
  <pre><code>@Entity
  @Table(name = "book")
  public class Book implements Serializable{
  
      private static final long serialVersionUID = 1L;
  
      private Long id;
      private String author;
      private String title;
  
      public Book(){}
      public Book(String author,String title){
          this.author = author;
          this.title = title;
      }
  
      @Id
      @GeneratedValue
      @Column(name = "BOOK_ID")
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      @Column(name = "author")
      public String getAuthor() {
          return author;
      }
      public void setAuthor(String author) {
          this.author = author;
      }
  
      @Column(name="title")
      public String getTitle() {
          return title;
      }
      public void setTitle(String title) {
          this.title = title;
      }
      //equals and hashcode ommited.
  }
  </code></pre>
  
  <p>Reader</p>
  
  <pre><code>@Entity
  @Table(name = "reader")
  public class Reader implements Serializable{
  
      private static final long serialVersionUID = 1L;
  
      private Long id;
      private String firstName;
      private String lastName;
      Set&lt;Book&gt; set = new HashSet&lt;Book&gt;();
  
      @Transient
      public void loanBook(Book book){
          set.add(book);
      }
  
      @Id
      @GeneratedValue
      @Column(name = "READER_ID")
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      @Column(name="firstName")
      public String getFirstName() {
          return firstName;
      }
  
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
  
      @Column(name="lastName")
      public String getLastName() {
          return lastName;
      }
  
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      @OneToMany(cascade=CascadeType.ALL,fetch = FetchType.LAZY)
       @JoinColumn(name="READER_ID")
      public Set&lt;Book&gt; getSet() {
          return set;
      }
  
  
      public void setSet(Set&lt;Book&gt; set) {
          this.set = set;
      }
  
      public Reader(){}
  
      public Reader(String firstName, String lastName){
          this.firstName = firstName;
          this.lastName = lastName;
      }
  }
  </code></pre>
  
  <p>Now I would like to fetch reader class using some book id for example:</p>
  
  <pre><code>Reader reader = service.getReaderbyBook(Long.valueOf(10));
  </code></pre>
  
  <p>My function looks like:</p>
  
  <pre><code>    @Override
      public Reader getReaderbyBook(Long id) {
          Session session = null;
          Reader reader = null;
           session =sessionFactory.openSession();
             org.hibernate.Transaction tr = session.getTransaction();
             tr.begin();
             String hqlSelect = "Select .... where book.id:=id";
             Query query = session.createQuery(hqlSelect);
             query.setLong("id", id);
             reader =  (Reader) query.uniqueResult();
             tr.commit();
             session.flush();
                session.close();
                return reader;
      }
  
  }
  </code></pre>
  
  <p>How my hql select should look like if I just want to fetch a single reader associated with some book? </p>