Skip to content

Instantly share code, notes, and snippets.

@jadhavj
Last active January 4, 2018 14:08
Show Gist options
  • Save jadhavj/49818ba13bd5ef2966923c87db2646d9 to your computer and use it in GitHub Desktop.
Save jadhavj/49818ba13bd5ef2966923c87db2646d9 to your computer and use it in GitHub Desktop.
Eg. GET '/v1/rms/property?startPage=5&pageSize=10'
YAML: /v1/rms/property:
get:
tags:
- Property
summary: Returns a list of properties
description: Returns a list of properties
operationId: GetPropertyList
consumes:
- application/json
parameters:
- in: query
name: startPage
description: Starting page to fetch records from
type: string
- in: query
name: pageSize
description: Page size
type: string
// JPA Code for Pagination
processGetPropertyListRequest(int startPage, int pageSize) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
countQuery.select(criteriaBuilder.count(countQuery.from(Foo.class)));
Long count = entityManager.createQuery(countQuery).getSingleResult();
CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(PropertyDetails.class);
Root<PropertyDetails> from = criteriaQuery.from(PropertyDetails.class);
CriteriaQuery<PropertyDetails> select = criteriaQuery.select(from);
TypedQuery<PropertyDetails> typedQuery = entityManager.createQuery(select);
while (startPage < count.intValue()) {
typedQuery.setFirstResult(pageNumber - 1);
typedQuery.setMaxResults(pageSize);
System.out.println("Current page: " + typedQuery.getResultList());
startPage += pageSize;
}
}
Eg. GET '/v1/rms/property/active/true?startPage=5&pageSize=10'
YAML: /v1/rms/property/active/{isActive}:
get:
tags:
- Property
summary: Returns a list of properties
description: Returns a list of properties
operationId: GetPropertyList
consumes:
- application/json
parameters:
- in: path
name: isActive
description: Starting page to fetch records from
type: boolean
- in: query
name: startPage
description: Starting page to fetch records from
type: string
- in: query
name: pageSize
description: Page size
type: string
// JPA Code for Pagination
processGetPropertyListRequest(boolean isActive, int startPage, int pageSize) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
countQuery.select(criteriaBuilder.count(countQuery.from(Foo.class)));
Long count = entityManager.createQuery(countQuery).getSingleResult();
CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(PropertyDetails.class);
Root<PropertyDetails> from = criteriaQuery.from(PropertyDetails.class);
CriteriaQuery<PropertyDetails> select = criteriaQuery.select(from);
TypedQuery<PropertyDetails> typedQuery = entityManager.createQuery(select);
while (startPage < count.intValue()) {
typedQuery.setFirstResult(pageNumber - 1);
typedQuery.setMaxResults(pageSize);
System.out.println("Current page: " + typedQuery.getResultList());
startPage += pageSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment