title: Criteria API: Fetch of a list returns repeated main entity
body: <p>I have the following Entities; Ticket contains a set of 0,N WorkOrder:</p>
  
  <pre><code>@Entity
  public class Ticket {
  
    ...
  
    @OneToMany(mappedBy="ticket", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List&lt;WorkOrder&gt; workOrders = null;
  
    ...
  }
  
  @Entity
  public class WorkOrder {
    ...
    @ManyToOne
    @JoinColumn(nullable = false)
    private Ticket ticket;
  }
  </code></pre>
  
  <p>I am loading Tickets and fetching the attributes. All of the 0,1 attributes present no problem. For workOrders, I used <a href="https://stackoverflow.com/questions/5024158/criteria-api-root-fectch-how-to-fetch-a-collection">this answer</a> to get the following code.</p>
  
  <pre><code>CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
  CriteriaQuery&lt;Ticket&gt; criteriaQuery = criteriaBuilder
    .createQuery(Ticket.class);
  Root&lt;Ticket&gt; rootTicket = criteriaQuery.from(Ticket.class);
  
  ListAttribute&lt;? super Ticket, WorkOrder&gt; workOrders =
    rootTicket.getModel().getList("workOrders", WorkOrder.class);
  rootTicket.fetch(workOrders, JoinType.LEFT);
  
      // WHERE logic
      ...
  
  criteriaQuery.select(rootTicket);
  TypedQuery&lt;Ticket&gt; query = this.entityManager.createQuery(criteriaQuery);
  return query.getResultList();
  </code></pre>
  
  <p>The result is that, in a query that should return me 1 Ticket with 5 workOrders, I am retrieving the same Ticket 5 times.</p>
  
  <p>If I just make the workOrders an Eager Fetch and delete the fetch code, it works as it should.</p>
  
  <p>Can anyone help me? Thanks in advance.</p>
  
  <p>UPDATE: </p>
  
  <p>One explanation about why I am not just happy with JB Nizet's answer (even if in the end it works).</p>
  
  <p>When I just make the relationship eager, JPA is examining exactly the same data that when I make it lazy and add the fetch clause to the Criteria / JPQL. The relationships between the various elements is also clear, as I define the <code>ListAttribute</code> for the Criteria query.</p>
  
  <p>There is some reasonable explanaition for the reason that JPA does not return the same data in both cases?</p>
  
  <p>UPDATE FOR BOUNTY: While JB Nizet's answer did solve the issue, I still find it meaningless that, given two operations with the same meaning ("Get <code>Ticket</code> and fetch all <code>WorkOrder</code> inside <code>ticket.workOrders</code>"), doing them by an eager loading needs no further changes while specifying a fetch requires a <code>DISTINCT</code> command</p>