Aside from initializing the SAP HANA database, the cds deploy
command created a file with the name default-env.json
in your bookstore
root folder. This file contains a set of credentials to connect to the SAP HANA HDI container, that was created by the command. CAP Java is able to automatically pick up the SAP HANA credentials from this file and configure the application running locally to use the SAP HANA HDI container as the database.
When deploying the application to the cloud, Cloud Foundry will provide the credentials as a service binding to the application through the Open Service Broker API. Also in this case, CAP Java will automatically pick up the SAP HANA credentials and configures the application for you as you’ll see in the next tutorial.
The described features are available as a plugin in CAP Java. Therefore, we’ll add an additional Maven dependency to your project. The dependency will bring the ability to read SAP HANA service bindings from the default-env.json to automatically configure the SAP HANA connectivity. In addition, it includes the SAP HANA JDBC driver.
-
Navigate back to the File Explorer by clicking on the corresponding icon.
-
Edit the pom.xml
in the srv
directory (not the pom.xml
file located in the root project folder) and add the following dependency under the <dependencies>
tag and make sure you Save the file:
<dependency>
<groupId>com.sap.cds</groupId>
<artifactId>cds-feature-hana</artifactId>
</dependency>

-
Before starting your application, make sure that you stop any running instances in the terminal or debug side panel.
-
Make sure that you are in the root of the bookstore project. Therefore, run the following command in the terminal:
cd ~/projects/bookstore
-
Let’s test the SAP HANA connectivity. Start your application by running:
mvn spring-boot:run -Dspring-boot.run.profiles=cloud
The Java system property -Dspring-boot.run.profiles=cloud
ensures that the default configuration using SQLite as the database, which is still defined in the application.yaml
, doesn’t get activated.
You can observe the log lines Loaded default-env.json from directory '/home/user/projects/bookstore'
and Registered primary 'DataSource' bean definition for connected service 'bookstore-hana'
, which indicate that the SAP HANA configuration was picked up.
-
Try the following example request, which creates an order together with its items through a deep insert. Open a new terminal by choosing Terminal > New Terminal from the main menu.
-
Create a new order together with items:
curl -X POST http://localhost:8080/odata/v4/OrdersService/Orders \
-H "Content-Type: application/json" \
-d '{ "currency_code": "USD", "items": [ { "book_ID": "b7bca6dd-0497-465e-9a5a-56f244174c8c", "amount": 1 } ] }'
The expected output should be the created database record similar to the one below:
{"@context":"$metadata#Orders(items())/$entity","ID":"67213f57-0b4c-43fc-8ed9-44f87ffb8967","createdAt":"2020-03-30T13:22:37Z","createdBy":"anonymous","modifiedAt":"2020-03-30T13:22:37Z","modifiedBy":"anonymous","total":14.14,"currency_code":"USD","items":[{"ID":"6352853e-f8e0-43b0-9c38-e15eb56a57ae","book_ID":"b7bca6dd-0497-465e-9a5a-56f244174c8c","amount":1,"netAmount":14.14,"parent_ID":"67213f57-0b4c-43fc-8ed9-44f87ffb8967"}]}