title: Hibernate subselect fetching of association, how to pass parameters?
body: <p><br/>
     We have a class A that has two levels of nested associations as follows:</p>
  
  <pre><code>@Entity
  @NamedQuery("select a from A JOIN FETCH b");
  public class A {
       @OneToOne(cascade = CascadeType.ALL, optional = true)
       @JoinColumn(name = "XXX")
       private B b;
  }
  
  @Entity
  public class B {
       @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
       @JoinColumn(name = "XXX")
       @Fetch(FetchMode.SUBSELECT)
       private List&lt;B&gt; details = new ArrayList&lt;&gt;(); 
  }
  
  @Entity
  public class C {
    ...
  }
  </code></pre>
  
  <p>Now for a report query we are fetching all A records, so I do a two-step query first I use pagination using <code>setMaxResults</code> and <code>setFirstResult</code> and then use the IDs of fetched objects to fire the second query where I want the entire object graph for the paginated object IDs.
     Now since Hibernate does not seem to allow to SUBSELECT for OneToOne, to fetch records of B, I resorted to JOIN FETCH (would prefer SUBSELECT here also if anyone knows how to do it, please share) but table C is much larger and I want to use SUBSELECT than join. Without the SUBSELECT, it fires many queries but uses the single IN parameter I specify and passes. But with SUBSELECT, it seems to expect the same IN parameter again,</p>
  
  <p>as in real SQL queries are like</p>
  
  <pre><code>select a.id from a limit ?  (fetch IDs by pagination)
  select a.*, b.* from a inner join b on a.b_id=b.id where a.id in (?, ?)
  select c.* from c where c.id in (select c_id from b where b.id in (Select b_id from a where id in (?, ?)))  - (this is fired by the SUBSELECT,  how to resend same named parameter say :ids a second time for this scenario?)
  </code></pre>
  
  <p>My code is like this:</p>
  
  <pre><code>List&lt;Long&gt; ids = fetchIds(startPos, maxRecords);
  TypedQuery&lt;A&gt; query =em.createNamedQuery(A.NAMED_QUERY, A.class);
  query.setParameter("ids", ids);
  </code></pre>
  
  <p>Assume that the <code>fetchIds</code> fetches the IDs in first step by pagination as described (no issues there).</p>
  
  <p>Thanks,<br/>
  Paddy</p>