Sunday, 22 September 2019

Spring Data: Simple Query builder with Specification, pagination and sorting


In this post, I am going to explain how can we construct simple query builder that supports pagination and sorting using Specification API.




Client submit a json query like below.
{
  "pageNumber": 0,
  "pageSize": 3,
  "searchFitler": [
    {
      "operator": "=",
      "property": "age",
      "value": 32
    }
  ],
  "sortOrder": {
    "ascendingOrder": [
      "firstName"
    ],
    "descendingOrder": [
      "lastName"
    ]
  }
}

Server will understand above query and return the output.

Example Query 1
{
  "pageNumber": 0,
  "pageSize": 3,
  "searchFitler": [
    {
      "operator": "=",
      "property": "age",
      "value": 32
    }
  ],
  "sortOrder": {
    "ascendingOrder": [
      "firstName"
    ],
    "descendingOrder": [
      "lastName"
    ]
  }
}


Example Query 2
{
  "pageNumber": 0,
  "pageSize": 3,
  "searchFitler": [
    {
      "operator": "=",
      "property": "age",
      "value": 32
    }
  ],
  "sortOrder": {
    "descendingOrder": [
      "firstName"
    ]
  }
}


We can solve above problem using PageRequest and Specification.
public class EmployeeService {

 @Autowired
 private EmployeeRepository empRepo;

 public List<Employee> findAll(SearchQuery searchQuery) {

  Specification<Employee> spec = SpecificationUtil.bySearchQuery(searchQuery, Employee.class);
  PageRequest pageRequest = getPageRequest(searchQuery);

  Page<Employee> page = empRepo.findAll(spec, pageRequest);

  return page.getContent();
 }

 private PageRequest getPageRequest(SearchQuery searchQuery) {

  int pageNumber = searchQuery.getPageNumber();
  int pageSize = searchQuery.getPageSize();

  List<Sort.Order> orders = new ArrayList<>();

  List<String> ascProps = searchQuery.getSortOrder().getAscendingOrder();

  if (ascProps != null && !ascProps.isEmpty()) {
   for (String prop : ascProps) {
    orders.add(Sort.Order.asc(prop));
   }
  }

  List<String> descProps = searchQuery.getSortOrder().getDescendingOrder();

  if (descProps != null && !descProps.isEmpty()) {
   for (String prop : descProps) {
    orders.add(Sort.Order.desc(prop));
   }

  }

  Sort sort = Sort.by(orders);

  return PageRequest.of(pageNumber, pageSize, sort);

 }

}

Find the below working application.


Employee.java
package com.sample.app.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@Table(name = "my_employee")
@EntityListeners(AuditingEntityListener.class)
public class Employee {
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Integer id;

 @Column(name = "first_name")
 private String firstName;

 @Column(name = "last_name")
 private String lastName;

 @Column(name = "age")
 private Integer age;

 @Column(name = "salary")
 private Double salary;

 @CreatedDate
 @Column(name = "created_date")
 private Date createdTime;

 @LastModifiedDate
 @Column(name = "last_modified_date")
 private Date lastModifiedTime;

 @CreatedBy
 private String createdBy;

 @LastModifiedBy
 private String modifiedBy;

 public Integer getId() {
  return id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public Integer getAge() {
  return age;
 }

 public void setAge(Integer age) {
  this.age = age;
 }

 public Double getSalary() {
  return salary;
 }

 public void setSalary(Double salary) {
  this.salary = salary;
 }

 public static EmployeeBuilder builder() {
  return new EmployeeBuilder();
 }

 public Date getCreatedTime() {
  return createdTime;
 }

 public void setCreatedTime(Date createdTime) {
  this.createdTime = createdTime;
 }

 public Date getLastModifiedTime() {
  return lastModifiedTime;
 }

 public void setLastModifiedTime(Date lastModifiedTime) {
  this.lastModifiedTime = lastModifiedTime;
 }

 public static class EmployeeBuilder {
  private Employee emp;

  public EmployeeBuilder() {
   emp = new Employee();
  }

  public EmployeeBuilder firstName(String firstName) {
   emp.setFirstName(firstName);
   return this;
  }

  public EmployeeBuilder lastName(String lastName) {
   emp.setLastName(lastName);
   return this;
  }

  public EmployeeBuilder age(int age) {
   emp.setAge(age);
   return this;
  }

  public EmployeeBuilder salary(double salary) {
   emp.setSalary(salary);
   return this;
  }

  public Employee build() {
   return emp;
  }
 }

 public String getCreatedBy() {
  return createdBy;
 }

 public void setCreatedBy(String createdBy) {
  this.createdBy = createdBy;
 }

 public String getModifiedBy() {
  return modifiedBy;
 }

 public void setModifiedBy(String modifiedBy) {
  this.modifiedBy = modifiedBy;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
    .append(lastName).append(", age=").append(age).append(", salary=").append(salary)
    .append(", createdTime=").append(createdTime).append(", lastModifiedTime=").append(lastModifiedTime)
    .append(", createdBy=").append(createdBy).append(", modifiedBy=").append(modifiedBy).append("]");
  return builder.toString();
 }

}


SearchFilter.java
package com.sample.app.model;

public class SearchFilter {
 private String property;
 private String operator;
 private Object value;

 public String getProperty() {
  return property;
 }

 public void setProperty(String property) {
  this.property = property;
 }

 public String getOperator() {
  return operator;
 }

 public void setOperator(String operator) {
  this.operator = operator;
 }

 public Object getValue() {
  return value;
 }

 public void setValue(Object value) {
  this.value = value;
 }

}


SearchQuery.java
package com.sample.app.model;

import java.util.List;

public class SearchQuery {
 private List<SearchFilter> searchFitler;

 private int pageNumber;
 private int pageSize;

 private SortOrder sortOrder;

 public List<SearchFilter> getSearchFitler() {
  return searchFitler;
 }

 public void setSearchFitler(List<SearchFilter> searchFitler) {
  this.searchFitler = searchFitler;
 }

 public int getPageNumber() {
  return pageNumber;
 }

 public void setPageNumber(int pageNumber) {
  this.pageNumber = pageNumber;
 }

 public int getPageSize() {
  return pageSize;
 }

 public void setPageSize(int pageSize) {
  this.pageSize = pageSize;
 }

 public SortOrder getSortOrder() {
  return sortOrder;
 }

 public void setSortOrder(SortOrder sortOrder) {
  this.sortOrder = sortOrder;
 }

}


SortOrder.java
package com.sample.app.model;

import java.util.List;

public class SortOrder {

 private List<String> ascendingOrder;

 private List<String> descendingOrder;

 public List<String> getAscendingOrder() {
  return ascendingOrder;
 }

 public void setAscendingOrder(List<String> ascendingOrder) {
  this.ascendingOrder = ascendingOrder;
 }

 public List<String> getDescendingOrder() {
  return descendingOrder;
 }

 public void setDescendingOrder(List<String> descendingOrder) {
  this.descendingOrder = descendingOrder;
 }

}


EmployeeRepository.java
package com.sample.app.repository;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.sample.app.model.Employee;

public interface EmployeeRepository extends JpaSpecificationExecutor<Employee>, PagingAndSortingRepository<Employee, Integer>{

}


EmployeeService.java
package com.sample.app.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import com.sample.app.model.Employee;
import com.sample.app.model.SearchQuery;
import com.sample.app.repository.EmployeeRepository;
import com.smaple.app.jpa.specification.SpecificationUtil;

@Component
public class EmployeeService {

 @Autowired
 private EmployeeRepository empRepo;

 public List<Employee> findAll(SearchQuery searchQuery) {

  Specification<Employee> spec = SpecificationUtil.bySearchQuery(searchQuery, Employee.class);
  PageRequest pageRequest = getPageRequest(searchQuery);

  Page<Employee> page = empRepo.findAll(spec, pageRequest);

  return page.getContent();
 }

 private PageRequest getPageRequest(SearchQuery searchQuery) {

  int pageNumber = searchQuery.getPageNumber();
  int pageSize = searchQuery.getPageSize();

  List<Sort.Order> orders = new ArrayList<>();

  List<String> ascProps = searchQuery.getSortOrder().getAscendingOrder();

  if (ascProps != null && !ascProps.isEmpty()) {
   for (String prop : ascProps) {
    orders.add(Sort.Order.asc(prop));
   }
  }

  List<String> descProps = searchQuery.getSortOrder().getDescendingOrder();

  if (descProps != null && !descProps.isEmpty()) {
   for (String prop : descProps) {
    orders.add(Sort.Order.desc(prop));
   }

  }

  Sort sort = Sort.by(orders);

  return PageRequest.of(pageNumber, pageSize, sort);

 }

}


SpeicifcationUtil.java
package com.smaple.app.jpa.specification;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.sample.app.model.SearchFilter;
import com.sample.app.model.SearchQuery;

public class SpecificationUtil {
 public static <T> Specification<T> bySearchQuery(SearchQuery searchQuery, Class<T> clazz) {

  return (Root<T> root, CriteriaQuery<?> query, CriteriaBuilder criterailBuilder) -> {

   List<Predicate> predicates = new ArrayList<>();
   List<SearchFilter> searchFilters = searchQuery.getSearchFitler();

   if (searchFilters == null || searchFilters.isEmpty()) {
    return null;
   }

   for (final SearchFilter searchFilter : searchFilters) {
    String property = searchFilter.getProperty();
    Path expression = root.get(property);

    switch (searchFilter.getOperator()) {
    case "=":
     predicates.add(criterailBuilder.equal(expression, searchFilter.getValue()));
     break;
    case "LIKE":
     predicates.add(criterailBuilder.like(expression, "%" + searchFilter.getValue() + "%"));
     break;
    case "IN":
     predicates.add(criterailBuilder.in(expression).value(searchFilter.getValue()));
     break;
    case ">":
     predicates.add(criterailBuilder.greaterThan(expression, (Comparable) searchFilter.getValue()));
     break;
    case "<":
     predicates.add(criterailBuilder.lessThan(expression, (Comparable) searchFilter.getValue()));
     break;
    case ">=":
     predicates.add(
       criterailBuilder.greaterThanOrEqualTo(expression, (Comparable) searchFilter.getValue()));
     break;
    case "<=":
     predicates
       .add(criterailBuilder.lessThanOrEqualTo(expression, (Comparable) searchFilter.getValue()));
     break;
    case "!":
     predicates.add(criterailBuilder.notEqual(expression, searchFilter.getValue()));
     break;
    case "IsNull":
     predicates.add(criterailBuilder.isNull(expression));
     break;
    case "NotNull":
     predicates.add(criterailBuilder.isNotNull(expression));
     break;
    default:
     System.out.println("Predicate is not matched");
     throw new IllegalArgumentException(searchFilter.getOperator() + " is not a valid predicate");
    }

   }

   if (predicates.isEmpty()) {
    return criterailBuilder.conjunction();
   }

   return criterailBuilder.and(predicates.toArray(new Predicate[0]));

  };
 }
}


EmployeeController.java
package com.sample.app.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.sample.app.model.Employee;
import com.sample.app.model.SearchQuery;
import com.sample.app.service.EmployeeService;

import io.swagger.annotations.ApiOperation;

@RestController
public class EmployeeController {

 @Autowired
 private EmployeeService empService;

 @ApiOperation(value = "Employees Filter", notes = "Get employees by search criteria")
 @PostMapping(value = "/employees", produces = MediaType.APPLICATION_JSON_VALUE)
 public ResponseEntity<List<Employee>> getEmployees(@RequestBody SearchQuery searchQuery) {

  List<Employee> emps = empService.findAll(searchQuery);

  return new ResponseEntity<>(emps, HttpStatus.OK);
 }

}


SwaggerConfig.java
package com.sample.app.config;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Component
@EnableAutoConfiguration
@EnableSwagger2
public class SwaggerConfig {

 @Bean
 public Docket userApi() {

  return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().paths(PathSelectors.any())
    .apis(RequestHandlerSelectors.basePackage("com.sample.app.controller")).build();
 }

 private ApiInfo apiInfo() {

  return new ApiInfoBuilder().title("Query builder").description("Query builder using spring specification")
    .version("2.0").build();
 }
}


App.java
package com.sample.app;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.sample.app.model.Employee;
import com.sample.app.repository.EmployeeRepository;

@SpringBootApplication
public class App {

 public static void main(String args[]) {
  SpringApplication.run(App.class, args);
 }

 @Bean
 public CommandLineRunner demo(EmployeeRepository employeeRepository) {
  return (args) -> {
   Employee emp1 = Employee.builder().firstName("Ram").lastName("Gurram").age(32).salary(100000.23).build();
   Employee emp2 = Employee.builder().firstName("Joel").lastName("Chelli").age(32).salary(60000).build();
   Employee emp3 = Employee.builder().firstName("Gopi").lastName("Battu").age(32).salary(1000000).build();
   Employee emp4 = Employee.builder().firstName("Bomma").lastName("Srikanth").age(39).salary(60000).build();
   Employee emp5 = Employee.builder().firstName("Surendra").lastName("Gurram").age(32).salary(100000.23).build();
   Employee emp6 = Employee.builder().firstName("Bhadri").lastName("Venakata RamaRao").age(32)
     .salary(100000.23).build();
   Employee emp7 = Employee.builder().firstName("Sushmithaa").lastName("Kulakarni").age(32).salary(100000.23)
     .build();

   employeeRepository.save(emp1);
   employeeRepository.save(emp2);
   employeeRepository.save(emp3);
   employeeRepository.save(emp4);
   employeeRepository.save(emp5);
   employeeRepository.save(emp6);
   employeeRepository.save(emp7);

   for (Employee emp : employeeRepository.findAll()) {
    System.out.println(emp);
   }

  };
 }
}


application.properties
logging.level.root=WARN
logging.level.org.hibernate=ERROR

## H2 specific properties
spring.h2.console.enabled=true
spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:file:~/db/myOrg.db;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;

spring.datasource.username=krishna
spring.datasource.password=password123

spring.datasource.driverClassName=org.h2.Driver

## JPA specific properties
# Creates the schema, destroying previous data.
spring.jpa.hibernate.ddl-auto=create

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.show-sql=true
#spring.jpa.properties.hibernate.format_sql=true

## Database connection pooling properties
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.max-wait=10000

# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.tomcat.max-active=10
spring.datasource.tomcat.max-idle=5
spring.datasource.tomcat.min-idle=3


pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>springDataSpecification</groupId>
 <artifactId>springDataSpecification</artifactId>
 <version>1</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>

  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>


  <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
  <dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>


  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
  </dependency>


  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2</version>
  </dependency>



 </dependencies>
</project>


Total project structure looks like below.

Run App.java.

Open swagger ui by hitting the url ‘http://localhost:8080/swagger-ui.html#/’.


Try with below input
{
  "pageNumber": 0,
  "pageSize": 3,
  "searchFitler": [
    {
      "operator": "=",
      "property": "age",
      "value": 32
    }
  ],
  "sortOrder": {
    "ascendingOrder": [
      "firstName"
    ],
    "descendingOrder": [
      "lastName"
    ]
  }
}


You can download source code from this link.


Previous                                                    Next                                                    Home

1 comment:

  1. Use below snippet to add to support not in query.

    case "NotIn":
    predicates.add(criterailBuilder.in(expression).value(searchFilter.getValue()).not());
    break;

    ReplyDelete