Can I use HQL to get the index of an ordered query result?
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.
For example, given:
<hibernate-mapping>
<class name="Dog" table="DOG">
<id name="id" type="int" column="DOG_id">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="age" type="int" />
<property name="name" type="string" />
</class>
</hibernate-mapping>
then these types of queries:
//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<Dog> dogList = dogQuery.list();
int idx = dogList.indexOf( 43 );
or, a more refined search through the dog list:
Query dogQuery = sess.createQuery("select d.id from Dog as d where (d.name='fluffy' OR d.age >= 3) order by d.age");
List<Dog> dogList = dogQuery.list();
int idx = dogList.indexOf( 43 );
The drawback here is that we are loading every dog.id into memory.
Scenario where I would need this:
- 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