HQL - row identifier for pagination

前端 未结 3 1750
慢半拍i
慢半拍i 2020-12-01 08:51

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(

3条回答
  •  鱼传尺愫
    2020-12-01 09:32

    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.

提交回复
热议问题