#1
This guide shows how to implement pagination and sorting in a Spring Boot app using SAP HANA Express.

1. Project Setup

Generate a Spring Boot project with dependencies: Spring Web, Spring Data JPA. Add SAP HANA JDBC driver (ngdbc.jar) into Maven as shown in earlier guides.

2. Configure application.properties

spring.datasource.url=jdbc:sap://localhost:39015/?databaseName=HXE
spring.datasource.username=SYSTEM
spring.datasource.password=YourPassword
spring.datasource.driver-class-name=com.sap.db.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.HANARowStoreDialect

3. Create Entity

import jakarta.persistence.*;

@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private double price;

    // getters and setters
}

4. Repository with Paging

import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductRepository extends JpaRepository<Product, Long> {
}

5. Service with Pagination and Sorting

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
public class ProductService {
    private final ProductRepository repo;

    public ProductService(ProductRepository repo) {
        this.repo = repo;
    }

    public Page<Product> getProducts(int page, int size, String sortBy) {
        return repo.findAll(PageRequest.of(page, size, Sort.by(sortBy)));
    }
}

6. Controller to Expose API

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/products")
public class ProductController {
    private final ProductService service;

    public ProductController(ProductService service) {
        this.service = service;
    }

    @GetMapping
    public Page<Product> getProducts(@RequestParam int page,
                                     @RequestParam int size,
                                     @RequestParam String sortBy) {
        return service.getProducts(page, size, sortBy);
    }
}

7. Run and Test

Start the app:
mvn spring-boot:run
Test with:
GET http://localhost:8080/products?page=0&size=5&sortBy=price
  • page=0 → first page
  • size=5 → five items per page
  • sortBy=price → sorted by product price

image quote pre code