Query query = entityManager.createQuery("SELECT e FROM Employee e"); query.setFirstResult(0); // retrieve the first result query.setMaxResults(10); // retrieve 10 results Listemployees = query.getResultList();
// assume there are 20 results in total int pageNum = 2; // the page number to retrieve int pageSize = 10; // the number of results to retrieve per page int firstResult = (pageNum - 1) * pageSize; // calculate the position of the first result Query query = entityManager.createQuery("SELECT e FROM Employee e"); query.setFirstResult(firstResult); query.setMaxResults(pageSize); ListThese examples demonstrate how setFirstResult() can be used to retrieve a specific subset of results from a query. This method is part of the javax.persistence package, which is included in the Java Persistence API (JPA) library.employees = query.getResultList();