title: How to use projection and where clause in JPA criteria?
body: <p>I have entity Person </p>
  
  <pre><code>@Entity(name = "Person")
  public class Person {
  
      @Id
      @GeneratedValue
      private Long id;
      private String name;
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")
      private Set&lt;Phone&gt; phones=new HashSet&lt;Phone&gt;();
  
      public Person() {
      }
  
      public Person(String name) {
          this.name = name;
  
      }
  </code></pre>
  
  <p>Ad entity Phone :</p>
  
  <pre><code>@Entity(name = "Phone")
  public class Phone {
  
      @Id
      @GeneratedValue
      private Long id;
  
      @Column(name = "`number`")
      private String number;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "person_id", nullable = false)
      private Person person;
  
      public Phone() {
      }
  </code></pre>
  
  <p>They have one-to-many relation.
  Now I want to build in jpa criteria such query:</p>
  
  <pre><code>select p.phones from person p join phone ph where p.name = :name;
  </code></pre>
  
  <p>So I want to extract <code>Set&lt;Phone&gt; phones</code> from Person entity where person's name is parameter.</p>
  
  <p>I've written this jpa criteria query:</p>
  
  <pre><code>CriteriaBuilder builder = session.getCriteriaBuilder();
          CriteriaQuery&lt;Person&gt; query = builder.createQuery(Person.class);
          Root&lt;Person&gt; root = query.from(Person.class);
          CriteriaQuery&lt;Person&gt; where = query.where(builder.equal(root.get("name"), "Mary Dick"));
          CompoundSelection&lt;Set&gt; projection = builder.construct(Set.class, root.get("phones"));
          where.select(projection); //compile error: The method select(Selection&lt;? extends Person&gt;) in the type CriteriaQuery&lt;Person&gt; is not applicable for the arguments (CompoundSelection&lt;Set&gt;)
      }
  </code></pre>
  
  <p>But it gives compile error:</p>
  
  <pre><code>The method select(Selection&lt;? extends Person&gt;) in the type CriteriaQuery&lt;Person&gt; is not applicable for the arguments (CompoundSelection&lt;Set&gt;)
  </code></pre>
  
  <p>How is it correct? Do I need metamodel classes?</p>