This guide shows how to run advanced queries using Spring Boot with SAP HANA Express. Follow the steps directly to practice.
1. Project Setup
Generate a Spring Boot project with Spring Initializr :
- Dependencies: Spring Web, Spring Data JPA
Add SAP HANA JDBC driver manually (download ngdbc.jar and install into Maven local repo as explained in previous tutorials).
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 Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String customer;
private double amount;
// getters and setters
}
4. Create Repository with Advanced Queries
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface OrderRepository extends JpaRepository<Order, Long> {
// Derived query
List<Order> findByCustomer(String customer);
// Range query
List<Order> findByAmountBetween(double min, double max);
// Custom JPQL query
@Query("SELECT o FROM Order o WHERE o.amount > :amount")
List<Order> findOrdersGreaterThan(double amount);
// Native SAP HANA SQL query
@Query(value = "SELECT * FROM ORDER WHERE CUSTOMER = ?1", nativeQuery = true)
List<Order> findByCustomerNative(String customer);
}
5. Test Queries
In DemoApplication.java:
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
CommandLineRunner run(OrderRepository repo) {
return args -> {
repo.save(new Order("Alice", 500));
repo.save(new Order("Bob", 1500));
System.out.println("Orders by Alice: " + repo.findByCustomer("Alice"));
System.out.println("High value orders: " + repo.findOrdersGreaterThan(1000));
};
}
}
Run with:
mvn spring-boot:run
image quote pre code