title: Select distinct with group by and having in HQL
body: <p>In my java project, I need to do an HQL query</p>
  
  <p>Here's my HQL query:</p>
  
  <pre><code>select count(distinct n.id)" +
              "  FROM Neighborhood n, NeighborhoodMeta meta, NeighborhoodAffordability aff, AirbnbProperty as ap" +
              "  WHERE n.id = meta.id AND n.id = aff.id AND n.id = ap.neighborhood AND aff.singleHomeValue!=null" +
              " AND (latitude &gt;=:minLat AND latitude &lt;=:maxLat)" +
              " AND (longitude &gt;=:minLong " + (meridian180WithinDistance ? "OR" : "AND") + " longitude &lt;=:maxLong) AND " +
              "acos(sin(:locationLatitude) * sin(radians(latitude)) + cos(:locationLatitude) * cos(radians(latitude)) * cos(radians(longitude) -:locationLongitude)) &lt;=:R " +
              "GROUP BY ap.neighborhood having count(ap.id) &gt; 19  
  </code></pre>
  
  <p>this count always produce a "1" result, however, if I deleted the last line of query, it returns a correct result, but I need to limit my results upon the having condition above.</p>
  
  <p>Could anybody help?</p>