Hitachi

uCosminexus Application Server Compatibility Guide


9.16.3 Specifying the range of query result items

You can obtain only an optionally specified item from multiple query results and only the query result specified in the starting position. To obtain this information, use the Query interface methods. This section describes the methods.

Organization of this subsection

(1) setMaxResults method

To obtain only an optionally specified item from multiple query results, use the setMaxResults method of the Query interface. The coding format of the setMaxResults method is as follows:

Query setMaxResults(int maximum-number-of-search-results)

In the method argument, specify the maximum number of search results.

(2) setFirstResult method

To obtain only the query result specified in the starting position, use the setFirstResult method. The coding format of the setFirstResult method is as follows:

Query setFirstResult(int starting-position-of-search-results)

In the method argument, specify the starting position of the search results. Specify a numeric value beginning with 0 as the starting position.

(3) Example usage of the Query interface methods

The examples of usage of the setMaxResults method and setFirstResult method are as follows. In this example, five Employee objects are obtained sequentially from the tenth employee with the highest monthly salary (e.monthlySalary) from the employee data (Employee).

Query query = em.createQuery( "SELECT e FROM Employee AS e " +
                              "ORDER BY e.monthlySalary DESC" )
                              .setFirstResult(9)
                              .setMaxResults(5);
List resultList = query.getResultList();

(4) Notes

When you use the setFirstResult method to specify the starting position, the time period from the invocation of the getResultList method and getSingleResult method until the obtaining of the result values varies depending on the value specified in the argument. Typically, the time taken until the result value returns is in proportion to the value of the starting position specified in the argument.