title: Spring PagingAndSortingRepository - find by name
body: <p><strong>Problem:</strong> I am trying to load into the grid only the Purchase Orders for which country name is...let's say Ireland. Thing is that I don't really know if that's possible. 
  So far I've tried to change the method in the service class <code>executeQueryFindAll()</code> to something like this.</p>
  
  <pre><code> private Page&lt;PurchaseOrder&gt; executeQueryFindAll(int page, int maxResults) {
      final PageRequest pageRequest = new PageRequest(page, maxResults, sortByNameASC());
  
      Page&lt;PurchaseOrder&gt; purchaseOrders = purchaseOrderRepository.findAll(pageRequest);
      Page&lt;PurchaseOrder&gt; selectedPurchaseOrders = new Page&lt;PurchaseOrder&gt;();  //not possibile to instantiate as Page is abstract
      for(PurchaseOrder next : purchaseOrders){
          if(next.getCountry().getCountryname().toString().equals("Ireland"))
              selectedPurchaseOrders.add(next); //thats lame because is not a LIST so it wont work
      }
      return selectedPurchaseOrders;
  
  }
  </code></pre>
  
  <p>The above approach wont work because Page is an abstract class, therefore cannot be instantiated. I can loop through but I can't do <code>selectedPurchaseOrders.add(next)</code> because that's not a List. In debug mode I can evaluate the <code>next.getCountry().getCountryname().toString()</code> and I get the country name and that's why I've tried to compare the String object <code>.equals("Ireland"))</code>.</p>
  
  <p>Any suggestion please?</p>
  
  <p><strong>Model</strong></p>
  
  <pre><code>@Entity
  @Table(name = "PURCHASEORDER",schema = "POTOOL")
  public class PurchaseOrder {
  
  @Id
  @GeneratedValue
  private int id;
  
  //other PurchaseOrder properties
  
  @ManyToOne(fetch=FetchType.EAGER)
  @JoinColumn(name = "country_id")
  private Country country;
  
  //Getter and Setters
  </code></pre>
  
  <p><strong>Repository</strong></p>
  
  <pre><code>public interface PurchaseOrderRepository extends
      PagingAndSortingRepository&lt;PurchaseOrder, Integer&gt; {
  
  Page&lt;PurchaseOrder&gt; findByTowerLike(Pageable pageable, String name);
  }
  </code></pre>
  
  <p><strong>Service</strong></p>
  
  <pre><code>@Service
  @Transactional
  public class PurchaseOrderService {
  
  @Autowired
  private PurchaseOrderRepository purchaseOrderRepository;
  
  @Transactional(readOnly = true)
  public PurchaseOrderListVO findAll(int page, int maxResults) {
      Page&lt;PurchaseOrder&gt; result = executeQueryFindAll(page, maxResults);
  
      if(shouldExecuteSameQueryInLastPage(page, result)){
          int lastPage = result.getTotalPages() - 1;
          result = executeQueryFindAll(lastPage, maxResults);
      }
  
      return buildResult(result);
  }
  
  private Page&lt;PurchaseOrder&gt; executeQueryFindAll(int page, int maxResults) {
      final PageRequest pageRequest = new PageRequest(page, maxResults,    sortByNameASC());
      return purchaseOrderRepository.findAll(pageRequest);
  
  }
  
  //rest of the class 
  </code></pre>