title: Spring Data JPA - Named query ignoring null parameters
body: <p>I have the following repository:</p>
  
  <pre><code>@Repository
  public interface EntityRepository extends JpaRepository&lt;Entity, Long&gt; {
  
      List&lt;Entity&gt; findAllByFirstId(Long firstId);
      List&lt;Entity&gt; findAllBySecondId(Long secondId);
      List&lt;Entity&gt; findAllByFirstIdAndSecondId(Long firstId, Long secondId);
  }
  </code></pre>
  
  <p>The constructor implementing an interface generated with <code>io.swagger:swagger-codegen-maven-plugin</code> uses <code>Optional&lt;Long&gt;</code> as optional request parameters (the underlying service uses also the same parameters):</p>
  
  <pre><code>ResponseEntity&lt;List&lt;Entity&gt;&gt; entities(Optional&lt;Long&gt; firstId, Optional&lt;Long&gt; secondId);
  </code></pre>
  
  <p>I would like to filter the entities based on the parameters <code>firstId</code> and <code>secondId</code> which are never <code>null</code>s at the database but can be passed through the constructor (the parameter for searching is optional). </p>
  
  <p>The problem comes with the named queries when the <code>null</code> is passed as the parameter is optional, the <code>JpaReposotory</code> uses the <code>null</code> as a criterion for the searching in the database. That's what I don't want - I want to ignore the filtering based on this parameter as long as it is <code>null</code>.</p>
  
  <p>My workaround solution based on <code>Optional</code> is:</p>
  
  <pre><code>public List&lt;Entity&gt; entities(Optional&lt;Long&gt; firstId, Optional&lt;Long&gt; secondId) {
  
      return firstId
          .or(() -&gt; secondId)
          .map(value -&gt; {
              if (firstId.isEmpty()) {
                  return entityRepository.findAllBySecondId(value);
              }
              if (secondId.isEmpty()) {
                  return entityRepository.findAllByFirstId(value);
              }
              return entityRepository.findAllByFirstIdAndSecondId(
              firstId.get(), secondId.get());
          })
          .orElse(entityRepository.findAll())
          .stream()
          .map(...)     // Mapping between DTO and entity. For sake of brevity
                        // I used the same onject Entity for both controler and repository 
                        // as long as it not related to the question   
  
          .collect(Collectors.toList());
  }
  </code></pre>
  
  <p>This issue has been already asked: <a href="https://stackoverflow.com/questions/43780226/spring-data-ignore-parameter-if-it-has-a-null-value">Spring Data - ignore parameter if it has a null value</a> and a ticket created <a href="https://jira.spring.io/browse/DATAJPA-209" rel="nofollow noreferrer">DATAJPA-209</a>.</p>
  
  <p>As long as the question is almost 3 years old and the ticket dates back to 2012, I would like to ask if there exists a more comfortable and universal way to avoid the overhead of handling the <code>Optional</code> and duplicating the repository methods. The solution for 2 such parameters looks acceptable, however I'd like to implement the very same filtering for 4-5 parameters.</p>