title: Implement search functionality for JPA and Spring boot
body: <p>I'm trying to implement properly search functionality for database table. I tried this approach:</p>
  
  <p>Controller:</p>
  
  <pre><code>    @GetMapping
      public Page&lt;TransactionDTO&gt; find(TransactionFilterDTO filter, Pageable page) {
          return searchRepository
                  .findTransactionsByFilter(mapper.toFilter(filter), page)
                  .map(mapper::toDTO);
      }
  </code></pre>
  
  <p>Filer DTO:</p>
  
  <pre><code>public class TransactionFilterDTO {
  
      private String name;
  
      private Integer id;
  
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
      private LocalDateTime from;
  
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
      private LocalDateTime to;
      ... // getters and setter
  }
  </code></pre>
  
  <p>Search implementation:</p>
  
  <pre><code>@Repository
  public class TransactionSearchRepositoryImpl implements TransactionSearchRepository {
  
      @Autowired
      private TransactionRepository transactionRepository;
  
      @Autowired
      private TransactionSpecification specification;
  
      @Override
      public Page&lt;Transaction&gt; findTransactionsByFilter(TransactionFilter filter, @Nullable Pageable page) {
  
  
          List&lt;Transaction&gt; transactions = transactionRepository
                  .findAll(specification.getFilter(filter));
  
          int totalCount = transactions.size();
  
          if(page != null) {
             transactions = transactions
                     .stream()
                     .skip(page.getOffset())
                     .limit(page.getPageSize())
                     .collect(Collectors.toList());
          }
  
          return new PageImpl&lt;&gt;(transactions, page, totalCount);
      }
  }
  </code></pre>
  
  <p>Repository:</p>
  
  <pre><code>public interface TransactionSearchRepository {
  
      Page&lt;Transaction&gt; findTransactionsByFilter(TransactionFilter filter, Pageable page);
  }
  </code></pre>
  
  <p>Is there some better way to implement a search functionality? This solution is very ugly in my view.</p>