title: Criteria query for unidirectional one-to-many relationship
body: <p>So, I have the following entities:</p>
  
  <pre><code>@Entity
  public class Supplier {
      @Column(name = "SUPPLIERID")
      private BigInteger supplierId;
  
      @OneToMany
      @JoinColumn(name = "ID_SUPP", foreignKey = @ForeignKey(name = "fk_POIS_SUPP"))
      private List&lt;POS&gt; posList;
  
      ...
  }
  
  @Entity
  public class POS {
      @Column(name = "POSID")
      private BigInteger posId
  }
  </code></pre>
  
  <p>So, <code>POS</code> does not have a reference to <code>Supplier</code>, which means that we have a unidirectional one-to-many relationship. I need to look for a <code>POS</code> by <code>posId</code> and <code>supplierId</code>. That is, find a supplier with the specified <code>supplierId</code> and then find a pos in the supplier's list of pos's that has the specified posId. How do I write a criteria query for this?</p>
  
  <p>I tried using subqueries. My idea was to create a subquery that would fetch all <code>POS</code>'s of a <code>Supplier</code> with a given <code>supplierId</code>. Then the main query would search within those <code>POS</code>'s for a <code>POS</code> with the given <code>posId</code>.</p>
  
  <p>The problem was I couldn't write a query that would fetch a <code>Supplier</code>s list of <code>POS</code>s. Apparently you can't write a query of type <code>List&lt;POS&gt;</code>:</p>
  
  <pre><code>CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  CriteriaQuery&lt;POS&gt; outerQuery = cb.createQuery(POS.class);
  Root&lt;POS&gt; outerQueryRoot = outerQuery.from(POS.class);
  
  Subquery&lt;POS&gt; subquery = outerQuery.subquery(POS.class);
  Root&lt;Supplier&gt; subqueryRoot = subquery.from(Supplier.class);
  subquery.where(cb.equal(subqueryRoot.get(Supplier_.supplierId), supplierId));
  subquery.select(subqueryRoot.get(Supplier_.posList);
  </code></pre>
  
  <p>On this last line, I get a compilation error that <code>Expression&lt;POS&gt; does not match Expression&lt;List&lt;POS&gt;&gt;</code>. And I can't change the type of the subquery because Java doesn't allow generic class literals (<code>List&lt;POS&gt;.class</code>).</p>
  
  <p>Any ideas?</p>