Hibernate - HQL pagination

后端 未结 6 1030
醉话见心
醉话见心 2020-12-30 00:04

This is a problem similar to: HQL - row identifier for pagination

I\'m trying to implement pagination using HQL. I have a PostgreSQL database.

int el         


        
6条回答
  •  隐瞒了意图╮
    2020-12-30 00:54

    i'm using this solution:

    /**
     * @param limitPerPage
     * @param page
     * @return
     */
    public List searchByPage(int limitPerPage, int page, String entity) {
        String sql = "SELECT t FROM " + entity + " t";
        Query query = em.createQuery(sql)
                .setFirstResult(calculateOffset(page, limitPerPage))
                .setMaxResults(limitPerPage);
        return query.getResultList();
    }
    
    /**
     * @param page
     * @return
     */
    private int calculateOffset(int page, int limit) {
        return ((limit * page) - limit);
    }
    

    Welcome.

提交回复
热议问题