Skip to Content

SAP HANA Native, Create Database Artifacts Using Core Data Services (CDS)

Use core data services to generate SAP HANA basic database artifacts.
You will learn
  • How to use core data services to create simple database entities
  • How to define database-agnostic artifacts in the persistence module
jung-thomasThomas JungJanuary 30, 2021
Created by
Lsubatin
July 25, 2019
Contributors
jung-thomas
Lsubatin

Prerequisites

  • This tutorial is designed for SAP HANA on premise and SAP HANA, express edition. It is not designed for SAP HANA Cloud.
  • You have created an application using the Business Application wizard.

The Cloud Application Programming model utilizes core data services to define artifacts in the database module. Because this model is meant to be database-agnostic – i.e., work with any database – it does not allow you to leverage features that are specific to SAP HANA. For this reason, you will create two tables that do not require any advanced data types.

  • Step 1

    In the db module, you will find a file called data-model.cds. Right-click on it and choose Rename.

    Create Database entities

    Use the following name:

    Name
    Copy
    interactions.cds
    

    Double-click on the file to open it.

    Create Database entities

    Replace the default contents with the following:

    CDS
    Copy
    namespace app.interactions;
    
    using { Country } from '@sap/cds/common';
    type BusinessKey : String(10);
    type SDate : DateTime;
    type LText : String(1024);
    
    
    entity Interactions_Header {
      key ID : Integer;
      ITEMS  : association to many Interactions_Items on ITEMS.INTHeader = $self;
      PARTNER  : BusinessKey;
      LOG_DATE  : SDate;
      BPCOUNTRY	: Country;
    
    };
    entity Interactions_Items {
    
    	key INTHeader : association to Interactions_Header;
    	key TEXT_ID : BusinessKey;
    		LANGU	: String(2);
    		LOGTEXT	: LText;
    };
    
    

    What is going on?

    You are declaring two entities with an association to each other. The design-time artifacts declared in this file will be converted to run-time, physical artifacts in the database. In this example, the entities will become tables.

  • Step 2

    Locate the other cds file (this file may be called cat-service.cds or my-service.cds, depending on the IDE) in the srv folder and rename it.

    Rename service

    Use the following name:

    Name
    Copy
    interaction_srv.cds
    

    Double-click to open it and replace the existing content with the following:

    CDS
    Copy

    using app.interactions from '../db/interactions'; service CatalogService { entity Interactions_Header @readonly as projection on interactions.Interactions_Header; entity Interactions_Items @readonly as projection on interactions.Interactions_Items; }

    Click Save all.

    Build database module

    What is going on?

    You are declaring services to expose the database entities you declared in the previous step.

    Open the package.json file in the root of your project. Add a section to the cds configuration to include the following

    JSON
    Copy
    "hana": {
       "deploy-format": "hdbtable"
    }
    
    Edit package.json

    Right-click on the CDS declaration of the services and choose Build > Build CDS.

    Build database module

    Look into the console to see the progress. You can scroll up and see what has been built

    Build database module
  • Step 3

    If you pay attention to the build log in the console, you will see the CDS artifacts were converted to hdbtable and hdbview artifacts. You will find those artifacts in a new folder under src called gen.

    Build database module

    You will now convert those CDS files specific to SAP HANA into runtime objects (tables). Right-click on the database module and choose Build.

    Build database module

    Note: If you get an error during the build process, this could be caused by an older version of the XSA Runtime in HANA, express edition. If your XSA version still contains Node.js version 8.x; this can lead to errors when building because this version of Node.js is no longer supported at the OS level. Newer versions of the XSA runtime only contain Node.js version 10.x and 12.x. If you do receive an error at this point, we would suggest editing the /db/package.json like the following to force the usage of the newer version of the Node.js runtime.

    json
    Copy
    {
    	"name": "deploy",
    	"dependencies": {
    		"@sap/hdi-deploy": "^3"
    	},
    	"scripts": {
    		"postinstall": "node .build.js",
    		"start": "node node_modules/@sap/hdi-deploy/deploy.js"
    	},
    	"engines": {
    		"node": "^10 || ^12"
    	}
    }
    

    Scroll up to in the console to see what the build process has done.

    What is going on?

    CDS stands for Core Data Services. This is an infrastructure that allows you to create persistency and services using a declarative language. Notice how you are using the same syntax to define both the persistency and the services.
     
    You can find more information on CDS in the help

    You defined a CDS artifact, this is an abstraction, a design-time declaration of the entities to be represented in a database and the services to expose them.
     

    Build database module

    The original .cds file was translated into hdbtable, which is the SQLDDL syntax specific to SAP HANA when you saved all of the files.
     

    Build database module

    These hdbtable files were then translated into runtime objects such as tables in the HANA database.

    Build database module

    If you checked the services in your space, you would see the service for your HDI container.

    Build database module

    or for the HANA Cloud trial:

    Build database module

    You can also check the resources in your space using the resource manager in SAP Web IDE:

    Build database module

    You can find a similar example and further context on Core Data and Services in this explanatory video

  • Step 4

    You can now check the generated tables and views in the database explorer. Right-click on the database module and select Open HDI Container.

    Build database module

    Once open, navigate to the Tables section and double-click on the Header table.

    Build database module

    Note the name of the table matches the generated hdbtable artifacts. You will also see the physical schema managed by the HDI container.

    Unless a name is specified during deployment, HDI containers are automatically created with names relative to the project and user generating them. This allows developers to work on different versions of the same HDI container at the same time.

    Build database module

  • Step 5

    Download the header file and the items file into your local file system.

    Right-click again on the header table and choose Import Data.

    Import data

    Browse for the Header file and click Step 2.

    Import data

    Keep the default table mapping and click Step 3.

    Import data

    Click Show Import Summary.

    Import data

    And then Import into Database.

    Import data

    You will see confirmation that 3 records have imported successfully.

    Import data

    Repeat the process with the Items.csv file into the Items table.

    Import data
  • Step 6

    You can now check the data loaded into the tables. Right-click on the Items table and click Generate Select Statement.

    Generate select statement

    Add the following WHERE clause to the SELECT statement and execute it to complete the validation below.

    SQL
    Copy
    where "LOGTEXT"  like '%happy%';
    

    Which results appear under `TEXT_ID`?

Back to top