title: Spring Data JPA: Sorting and paging with joined tables
body: <p>I have a scenario where I want to filter, sort and page over a result where 3 tables take part.</p>
  
  <p>At the moment I use Spring Data JPA's Specification feature to do it on a single entity: <code>repository.findAll(specification, pageRequest)</code>.</p>
  
  <p>This works great, but now I have another scenario where the sort / filter attributes are spread over 3 tables which are connected by one-to-many relations.</p>
  
  <p>Here is my scenario:</p>
  
  <pre><code>@Entity
  public class CustomerEntity ... {
    ...
  
    @Column(nullable = false)
    public String                                 customerNumber;
  
    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
    public List&lt;CustomerItemEntity&gt; items;
  }
  
  
  @Entity
  public class CustomerItemEntity ... {
    ...
  
    @Column(nullable = false)
    public String                                 itemNumber;
  
    @ManyToOne(optional = false)
    @JoinColumn(name = "customerId")
    public CustomerEntity customer;
  
    @OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval = true)
    public List&lt;DocumentEntity&gt; documents;
  }
  
  
  @Entity
  public class DocumentEntity ... {
    ...
  
    @Column(nullable = false)
    public LocalDate                                 validDate;
  
    @ManyToOne(optional = false)
    @JoinColumn(name = "itemId")
    public CustomerItemEntity item;
  }
  </code></pre>
  
  <p>Is there a way to use <code>PageRequest</code> and <code>Specification</code> where <code>customerNumber</code>, <code>itemNumber</code> and <code>validDate</code> are used for filtering, sorting and paging at the same time?</p>