Note that there are some explanatory texts on larger screens.

plurals
  1. POHQL to return the index of an object in an ordered query result?
    text
    copied!<p>Can I use HQL to get the index of an <em>ordered</em> query result? </p> <p>The only way I know to do this is to get back all of the results from the database, then iterate through all of the results.</p> <p>For example, given:</p> <pre><code>&lt;hibernate-mapping&gt; &lt;class name="Dog" table="DOG"&gt; &lt;id name="id" type="int" column="DOG_id"&gt; &lt;meta attribute="scope-set"&gt;protected&lt;/meta&gt; &lt;generator class="native"/&gt; &lt;/id&gt; &lt;property name="age" type="int" /&gt; &lt;property name="name" type="string" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>then these types of queries:</p> <pre><code>//determine where dog with id 43 is in the current search results for dogs ordered by age Query dogQuery = sess.createQuery("select d.id from Dog as d order by d.age"); List&lt;Dog&gt; dogList = dogQuery.list(); int idx = dogList.indexOf( 43 ); </code></pre> <p>or, a more refined search through the dog list:</p> <pre><code>Query dogQuery = sess.createQuery("select d.id from Dog as d where (d.name='fluffy' OR d.age &gt;= 3) order by d.age"); List&lt;Dog&gt; dogList = dogQuery.list(); int idx = dogList.indexOf( 43 ); </code></pre> <p>The drawback here is that we are loading every dog.id into memory. </p> <p><em>Scenario where I would need this:</em></p> <ul> <li>displaying a specific query result (of thousands) as a dot on a line. The line, and the dot, gets updated every minute or so. This visualization gives "real time" updates on the rank of a search query</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload