With the SAP Cloud Application Programming Model and its implementation of Core Data Services (CDS), we don’t directly import a Calculation View or other native DB artifact. This import, however, is important if you want to expose these artifacts via OData V4 services, since that requires a CDS entity or view.
But CDS does have an annotation called @cds.persistence.exists. This annotation allows you to re-define an existing DB object and CDS won’t attempt to create or alter it. It will just assume it already exists in the matching state.
This feature does require you to completely redefine the DB artifact with exactly the same name, columns and column names.
In this exercise, lets see how we can create a calculation view and other artifacts and expose them to CDS.
Calculation Views and other HANA native artifacts allow you to leverage HANA specific features and optimizations that might not otherwise be available at the abstraction layers within the SAP Cloud Application Programming Model. Calculation Views are especially good at aggregation and filtering of large datasets. In this exercise will create a simple join Calculation View based upon or small data set and data model. This is done so we focus on the mechanics of combining HANA native with CAP without needing the typical large data set where the technical advantages of Calculation Views become more apparent.
Create a new Calculation View via View > Command Pallette and then SAP HANA: Create SAP HANA Database Artifact command pallet entry.
New calculation view
Create a calculation view called V_INTERACTION of Data Category DIMENSION and Dimension Type of STANDARD. Press Create
Create calculation view
The new artifact is created in the /db/src folder. This way you can have a single HANA database model that contains both HANA native content and CAP generated content.
View created in the /db/src folder
Click on the V_INTERACTION.hdbcalculationview to load the graphical calculation view editor.
Calculation View editor
Step 2Model the join relationship
+
Drop a join node into the modeling space
Join Node
Use theplus signsign to add tables to the node.
Add data source
Type in HEADER and then select the table you created earlier via CDS called APP_INTERACTIONS_HEADER and press Finish.
Add header table
Repeat the process to add the APP_INTERACTIONS_ITEMS table to the same join node. You should see both artifacts in the join node.
Both tables in join
Double-click on the join node. A panel will open on the right.
Join Definition on the right
Drag and drop the ID field to the INTERACTION_ID field.
Create Join
Set the cardinality to 1..n
Cardinality
In the Mapping tab, add all the columns except ID* Columns as output columns.
Mapping
Connect the join node with the Projection node using thearrow
Connect to Projection #1
Connect to Projection #2
Click on the Projection node and double-click on the join parent to add all the columns to the output
Projection Join
From the SAP HANA Projects view, press the Deploy button
Deploy
Check the deployment log to make sure everything was successfully created in the database.
Check Log
Open the HDI Container in the Database Explorer
Open Database Explorer
Under Column Views you will find your Calculation View. Choose Open Data
Test Calculation View
Go to the Raw Data and you should see the header and item data joined together.
Raw Data of view
Step 3Create calculation view proxy entity
+
We now want to expose our Calculation View to the Cloud Application Programming model by creating a “proxy” entity for the view in the CDS data model.
Return to the Business Application Studio and open interactions.cds.
Edit interactions.cds
We need our proxy entity to be created without the namespace in our current interactions.cds. Therefore comment out the namespace line and add all the existing content except the using ... line in a new context for app.interactions.
Add Context
We need to add a matching entity definition for the Calculation View. This means redefining all the column names and data types / lengths. Doing so manually would be error prone, but the hana-cli has a utility that will help. Open a terminal and change to the db folder with the command cd db. Now issue the command:
Shell
hana-cli inspectView -v V_INTERACTION -o cds
inspectView
With this command you are looking up the definition of the view but asking for the output (-o) in the CDS format.
Copy this block from the terminal and paste it into the interactions.cds file at the end outside the context block.
Add Proxy entity
CDS does have an annotation called @cds.persistence.exists. This annotation allows you to re-define an existing DB object and CDS won’t attempt to create or alter it. It will just assume it already exists in the matching state.There is also the annotation @cds.persistence.calcview. This will further tell the Cloud Application Programming Model that this target entity is also a Calculation View.
Now open the interactions_srv.cds file from the /srv folder. Add this new Calculation View based entity to the CAP service as read-only.
Add Entity to Service
From the terminal return to the root of the project and issue the command: cds build --production
Shell
cds build --production
CDS build
Although we didn’t add any new database artifacts to the project, the addition of an entity to the service layer causes new views to be generated within SAP HANA. Therefore we need to deploy to the database using the SAP HANA Projects view before we can test.
Deploy
From the console in the project root hopefully you still have the cds watch ... running. Otherwise start it again with cds watch --profile hybrid to start the CAP service layer for testing. If you have performed the tutorial SAP HANA Cloud, Add User Authentication to Your Application, remember you must also run the application router to test your service with authentication.
You can test your view via the service layer by adding /odata/v4/catalog/V_Interaction to the path.
Test
Congratulations! You have now successfully combined HANA native artifacts with the SAP Cloud Application Programming Model and learned the modern HANA way to expose Calculation Views via OData.
Share feedback on this tutorial or join the conversation in SAP Community.
Submit detailed feedbackDiscuss in Community
Steps
Step 1 of 3
1. Create calculation view2. Model the join relationship3. Create calculation view proxy entity
Joule
AI Notice
Joule is an AI assistant. Generative AI may produce inaccurate, incomplete, or biased information. Always verify important details before acting on them.
Conversations are sent to SAP-hosted large language models for processing. Do not include personal data, credentials, or confidential information in your messages.
Joule's responses are based on the SAP tutorial catalog and may not reflect the latest product changes. For authoritative guidance, consult the linked tutorials and official SAP documentation.