title: java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
body: <p>This is my repository:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Repository
  public interface MyRepository extends JpaRepository&lt;Entity, Long&gt; {
  
    public static final String DISTANCE = "((acos(sin(?1 * pi() / 180) * sin(a.latitude * pi() / 180) + cos(?1 * pi() / 180) * cos(a.latitude * pi() / 180) * cos((?2 - a.longitude) * pi() / 180)) * 180 / pi()) * 60 * 1.609344) as distance";
  
    @Query("select new package.SearchResult(" + DISTANCE + ", a.addressOwner) from Address a group by a.addressOwner, col_0_0_ having col_0_0_ &lt; ?3 order by col_0_0_")
    public Page&lt;SearchResult&gt; findClosestByCoordinates(double lat, double lng, double maxDistance, Pageable pageable);
  
  }
  </code></pre>
  
  <p>When I try to execute this method an exception occurs:</p>
  
  <p><strong>Parameter with that position [1] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [1] did not exist']</strong></p>
  
  <p>But when I replace <strong><code>Page&lt;SearchResult&gt;</code></strong> with <strong><code>List&lt;SearchResult&gt;</code></strong> everything works just fine. Is it Spring's bug or something?</p>
  
  <p><strong>UPDATE</strong>: I guess I found what is wrong: when ALL the parameters take part in where clause, everything is ok. But if at least one of them is not used there, it fails. But I don't understand why it happens with Page and doesn't happen when using List. And what is the best way to deal with it?</p>