Skip to Content

XS Advanced, Create Database Artifacts Using Core Data and Services

test
0 %
XS Advanced, Create Database Artifacts Using Core Data and Services
Details
// Explore More Tutorials

XS Advanced, Create Database Artifacts Using Core Data and Services

07/29/2019

Beginner
15 min.
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

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: Create database entities

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:

interactions.cds

Double-click on the file to open it.

Create Database entities

Replace the default contents with the following:

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.

Log on to answer question
Step 2: Adapt service call

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:

interaction_srv.cds

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


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.

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
Log on to answer question
Step 3: Explore the generated design-time artifacts

If you pay attention to the build log in the console, you will see the CDS artifacts were converted to hdbcds 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

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 hdbcds, which is the Core Data and Services syntax specific to SAP HANA when you saved all of the files.
 
Build database module

These hdbcds 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

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

Log on to answer question
Step 4: Check the Database Explorer

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 hdbcds 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

Log on to answer question
Step 5: Load data into your tables

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, select Sheet 1 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.xlsx file into the Items table.

Import data
Log on to answer question
Step 6: Check data loaded into the tables

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.

where "LOGTEXT"  like '%happy%';
Which results appear under `TEXT_ID`?
×

Next Steps

Prerequisites

Back to top