title: How to impose LIMIT on sub-query of JPA query?
body: <p>Is it possible to impose LIMIT on sub-query in JPA query ?</p>
  
  <p>I have following query in pure SQL</p>
  
  <pre><code>select * from ipinfo 
  where RangeEnd &lt; (select RangeStart from ipinfo where RangeStart &gt;= 1537022421 order by RangeStart asc limit 1) and (1537022421 &lt;= RangeEnd)
  ORDER BY RangeEnd desc
  limit 1
  </code></pre>
  
  <p>Converting it directly to JPQL I'd have something like</p>
  
  <pre><code>select obj from IpInfo obj
  where obj.rangeEnd &lt; (select obj2.rangeStart from IpInfo obj2 where obj2.rangeStart &gt;= ?1 order by obj2.rangeStart asc limit 1) and (?1 &lt;= obj.rangeEnd)
  ORDER BY obj.rangeEnd desc
  limit 1
  </code></pre>
  
  <p>Since I can't use LIMIT in JPQL I'd have to use <code>setMaxResults(1)</code> on it. But what about sub-query?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I decided to go with <code>@NamedNativeQuery</code> for now but it's DB-specific code. If you guys can suggest pure JPA solution I'll really appreciate it.</p>