title: How to select a JPA Entity by whitelisting a property of its child Entity with the Criteria API?
body: <p>I'm stuck with the following hypothetical problem:</p>
  
  <p>Using the <strong>Criteria API</strong> (and not <strong>JPQL</strong>), and given</p>
  
  <ol>
  <li><p>A table full of <strong>users</strong>, each one having multiple <strong>cars</strong> </p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity 
  public class User {
      @Id 
      private Long     id;
  
      @OneToMany 
      private Set&lt;Car&gt; cars;
  }
  </code></pre>
  
  
  
  <pre class="lang-java prettyprint-override"><code>@Entity 
  public class Car {
      @Id 
      private Long id;
  
      private String model;
  }
  </code></pre></li>
  <li><p>A <code>Set&lt;String&gt;</code> containing <strong>car models</strong>:</p>
  
  <pre class="lang-java prettyprint-override"><code>Set&lt;String&gt; models = getThousandsOfModels();
  </code></pre></li>
  </ol>
  
  <blockquote>
    <p><strong>How can I select the <code>User</code>s having at least one <code>Car</code> whose <code>model</code> is in the <code>models</code> Set ?</strong></p>
  </blockquote>
  
  <p>I've read many SO answers, tried different ways (the most promising seemed to involve creating predicates with <code>Expression&lt;Collection&lt;String&gt;&gt;</code>, using <code>.in()</code> etc.) but nothing worked.</p>