9

Does anyone know if HQL has a keyword to identify rows such as ROWID or ROWNUM?

I would like to implement pagination with HQL but I am not able to use .setMaxResult() or .setFirstResult() because I don't work with the session object directly and therefore don't use the Query object but simply create my query as a string and use the .find() method.

I tried using LIMIT and OFFSET in my query, but HQL seems to be ignoring these keywords and is returning the whole result to me no matter what.

I'm also not able to use Hibernate criteria because it does not have support for the "HAVING" clause that appears in my query.

My last resort is to restrict the result set using the ROWNUM/ROWID keyword. Does anyone else have any other suggestions?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
user57701
  • 213
  • 2
  • 4
  • 7

3 Answers3

18

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.

Andreas Petersson
  • 16,248
  • 11
  • 59
  • 91
  • Yes it's possible but using scroll instead of list. You'll need to add the paging logic though (which Hibernate does for you). – Lluis Martinez Dec 08 '09 at 18:28
  • 1
    I think there is a small bug in that example, the first result should be set like: `.setFirstResult(elementsPerBlock * ((page-1) + 1))` – rcl Nov 09 '10 at 03:50
  • @Andreas Peterson and if I want to order by nullable column, how can I achieve that? as far as common solution in my example is to have an additional *rownum* column in Order By clause – edward_wong Jun 23 '16 at 17:35
2

Pagination using Query Interface:

There are two methods of the Query interface for pagination.

1. Query setFirstResult(int startPosition): This method takes an integer that represents the first row in your result set, starting with row 0.

2. Query setMaxResults(int maxResult): This method tells Hibernate to retrieve a fixed number maxResults of objects. Using above two methods together, we can construct a paging component in our web or Swing application.

Example:

Query query = session.createQuery("FROM Employee");
query.setFirstResult(5);
query.setMaxResults(10);
List<Employee> list = query.list();
for(Employee emp: list) {            
   System.out.println(emp);
}

Pagination using Criteria Interface: There are two methods of the Criteria interface for pagination.

1. Criteria setFirstResult(int firstResult):

Set the first result to be retrieved.

2. List item Criteria setMaxResults(int maxResults):

Set a limit upon the number of objects to be retrieved.

Example:

Criteria criteria = session.createCriteria(Employee.class);
criteria.setFirstResult(5);
criteria.setMaxResults(10);            
List<Employee> list = criteria.list();
for(Employee emp: list) {            
    System.out.println(emp);
}
Ranga Reddy
  • 2,936
  • 4
  • 29
  • 41
1

Well, you can in principle access ROWNUM/ROWID from HSQL (though I've never used it). See e.g. Ron's Weblog. That should work.

But I'd like to point out that you're really working against Hibernate and HSQL if you do it like that. The proper way is to use setMaxResult() & friends. If you cannot do that b/c of your architecture, I'd at least reconsider my architectural decisions. I know these are always tough changes to make, but it might be worth it.

sleske
  • 81,358
  • 34
  • 189
  • 227