HQL – row identifier for pagination

this is one situation where hibernate shines:

typical solution with hql query.

int elementsPerBlock = 10;
int page = 2;

return  getSession().createQuery("from SomeItems order by id asc")
            .setFirstResult(elementsPerBlock * (page-1) + 1 )
            .setMaxResults(elementsPerBlock)
            .list();

hibernate will translate this to a pattern that is understood by the database according to its sql dialect.
on oracle it will create a subselect with ROWNUM < X.
on postgresql it will issue a LIMIT / OFFSET
on msSQL server it will issue a TOP..

to my knowledge it is not possible to achieve this with “pure” hql.

Leave a Comment