Skip to Content
Technical Articles

Develop a Spring Boot (Java) application with HANA database on SAP Cloud Platform (Cloud Foundry) – PART 1

In this blog series, we will develop a Spring Boot application and deploy it on SAP Cloud Platform (Cloud Foundry). For persistence, we will make use of SAP HANA service on SCP. The goal of this project is to show how we can make use of HANA database in Cloud foundry. The application itself is very simple – REST endpoints supporting CRUD operations on Employee entity.

The code used in this blog can be found here – GitHub

So, let’s get started.

STEP 1 : Create the Spring boot application.

1.1 Create Spring Boot project using Spring Starter project in Eclipse

Install Spring Tools 4 for Eclipse.

Select New project -> Spring -> Create new Spring Boot project.

Group ID – spring-hana-cloud-foundry
Artifact ID – 
Name –
spring- hana-cloud-foundry
Description –
Sample Spring Boot application to use SAP HANA Service

1.2 Add the following dependencies, profiles and plugins in pom.xml

pom.xml –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath/> <!-- lookup parent from repository -->
	<description>Sample Spring Boot application to use SAP HANA Service</description>









  • spring-boot-starter-data-jpa​ – Starter for using Spring Data JPA with Hibernate
  • lombok – Provides automatic getter, setters and other convenient annotations for your POJOs.
  • h2 – In-memory database used for local testing

The important dependencies are –

  • spring-cloud-cloudfoundry-connector – It simplifies the process of connecting to services in cloud environments like Cloud Foundry.
  • spring-cloud-spring-service-connector – This library provides data source implementations for spring data connector.
  • spring-cloud-cloudfoundry-hana-service-connector – Hana connector for Spring boot.
  • ngdbc – HANA driver.


1.3 Create the Models, Repository, Service and Controller for the application

Create model –


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.Getter;
import lombok.AllArgsConstructor;

public class Employee {
	@Column(name ="id", unique=true)
	private long id;
	@Column(name ="firstName")
	private String firstName;
	@Column(name ="lastName")
	private String lastName;
	@Column(name ="email", unique=true)
	private String email;
	@Column(name ="contact",unique=true)
	private String contact;


Create corresponding repository for the model class –



public interface EmployeeRepository extends CrudRepository<Employee, Long>{

Create corresponding service for the repository –


import java.util.ArrayList;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class EmployeeService {

	private EmployeeRepository employeeRepository;
	public long getCount() {
		long count = employeeRepository.count();
		return count;
	public List<Employee> findAllEmployee(){
		List<Employee> employee = new ArrayList<>();
		return employee;
	public boolean insertEmployee(Employee employee) {
	 try {;
		return true;
	 catch (Exception e) {
		return false;
	public Employee findEmployeeById(Long id) {
		Employee employee = employeeRepository.findById(id).orElse(null);
		return employee;
	public boolean deleteEmployee(long id) {
		Employee employee = employeeRepository.findById(id).orElse(null);
		if(employee!=null) {
			return true;
		return false;


Create the controller to handle all the requests –


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

public class EmployeeController {
	Logger log = LoggerFactory.getLogger(getClass());
	private EmployeeService employeeService;
	public long count() {"Search total number of employees");
		return employeeService.getCount();
	public List<Employee> getAllEmployees(){"Searching all employees");
		return employeeService.findAllEmployee();
	@RequestMapping(method=RequestMethod.POST, value = "/employee/add")
	public boolean addEmployee(@RequestBody Employee employee) {"Creation/Updating Employee - "+employee.toString());
		return employeeService.insertEmployee(employee);
	@RequestMapping("/employee/id/{id}" )
	public Employee findById(@PathVariable long id) {"Searching employee with ID - "+ id);
		return employeeService.findEmployeeById(id);
	@RequestMapping(method=RequestMethod.DELETE, value="/employee/delete/{id}")
	public boolean deleteEmployee(@PathVariable long id) {
		return employeeService.deleteEmployee(id);


And finally we come to most important bit of code, the configuration of the datasource.

We will bind the datasource to the application on runtime. The datasource details are available as environment variables in CF  also known as – VCAP_SERVICES. At the time of writing this blog, the datasource could not be bound to the application directly. The url, username and password needed to be injected manually into the config class due to an issue with the ngdbc driver – Issue


Create a config class for the datasource as follows  –


import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.zaxxer.hikari.HikariDataSource;

public class CloudDatabaseConfig extends AbstractCloudConfig {
	public DataSource dataSource(@Value("${hana.url}")final String url,
			@Value("${hana.user}")final String user,
			@Value("${hana.password}")final String password) {
		return DataSourceBuilder.create()



1.4 Create application properties


With that we are done with our application logic. The final bit is the application configuration which would define how we connect to the database in cloud as well as in local.

For cloud , create properties file as –

spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy = org.hibernate.dialect.HANAColumnStoreDialect

hana.url = ${}
hana.user = ${}
hana.password = ${}


hana_migration is the name of the HANA service instance bound to the application (We will cover this topic in the next part).

And for local testing , create properties file as –



And in add –


With that, we are ready with our application code. The next step would be to deploy the application on cloud foundry and test it.


Additionally, if you want to run the application in local and test, you can run it as a Spring Boot app from Eclipse. The following endpoints are available for testing –

  1. GET /employee/count -> Returns count of total employees in database.
  2. GET /employee/all -> Returns all employees in database.
  3. GET /employee/id/{id} -> Returns the employee instance corresponding to the ID.
  4. POST /employee/add -> Add a new employee in database. Use the following payload in body –
  5. DELETE /employee/delete/{id} -> Delete particular employee from database.


See you in the next part where we configure the HANA instance in Cloud platform.



Find the remaining parts here –

  1. Develop the Spring Boot Application (PART 1) – Develop a Spring Boot (Java) application with HANA database on SAP Cloud Platform (Cloud Foundry) – PART 1
  2. Create instance of HANA service (PART 2) – Develop a Spring Boot (Java) application with HANA database on SAP Cloud Platform (Cloud Foundry) – PART 2
  3. Deploy and Test (PART 3) – Develop a Spring Boot (Java) application with HANA database on SAP Cloud Platform (Cloud Foundry) – PART 3
You must be Logged on to comment or reply to a post.
  • An even easier way to automatically get the credentials wired up is using CFJavaEnv

    Assuming you are using spring boot 2.x

    simply add


    to your pom and add the wiring:

    	public DataSourceProperties dataSourceProperties() {
    		CfJdbcEnv cfJdbcEnv = new CfJdbcEnv();
    		DataSourceProperties properties = new DataSourceProperties();
    		CfCredentials hanaCredentials = cfJdbcEnv.findCredentialsByTag("hana");
    		if (hanaCredentials != null) {
    			String uri = hanaCredentials.getUri("hana");
    		return properties;

    This is enough to wire up the datasource.

  • Thanks

    This library will be very helpful in parsing CF environment variables. I always wondered why don’t we have a parser library in Java when something similar already existed in Node (@SAP/xsenv and cfenv). I guess that problem is solved.