HQL - row identifier for pagination

前端 未结 3 1751
慢半拍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:14

    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 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 list = criteria.list();
    for(Employee emp: list) {            
        System.out.println(emp);
    }
    

提交回复
热议问题