Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
Showing results for 
Search instead for 
Did you mean: 
In this post I will explain how to handle temporal data in SAP Cloud Application Programming model  with HANA native artifacts.

Temporal data is time dependent. We can say if there is any timestamp associated with any data set it can be called as temporal. SAP Cloud Application Programming model provides, out of the box support to for temporal entities.

Two ways we can declare entities as temporal.

1.  With annotations @CDS.valid.from/to

We can just add these annotations to the date fields to in an entity where temporal data needs to be handled.
entity Product { 
validFrom : Date @cds.valid.from;
validTo : Date;

2. Declare an entity using aspect  temporal

Using this predefined aspect temporal we can record changes in the database with valid from/to boundaries. Deploying this cds artefacts in the database will create validFrom and validTo timestamp fields in the respective tables.
using { temporal } from '@sap/cds/common';

entity Product : temporal {
key productId : String(20);
productName : String(20);
productCategory : String(20);

Read requests on these entities will return valid data as of now without specifying any parameter.

With this approach we can see time slices are recorded in a single table. Many change requests with time, data size in single table will keep on increasing and cause performance issues.

We can consider the alternate approach to achieve this is by HANA System Versioned tables.=

System versioned tables with SAP Cloud Application Programming model :

System versioned tables support the tracking of changes on column store tables by capturing the validity period of each record.

System-versioned tables always consist of two physical tables:

  1. The main table of records that are currently valid.

  2. A corresponding history table (a one-to-one correspondence) of archived records. A naming convention is not required but may be helpful, for example, to append "_history" to the name of the main table.

The valid_from and valid_to columns are timestamps and are maintained by the system.

Below are the steps to achieve this.

Step 1:

Create a devspace in Business Application Studio selecting full stack cloud project including SAP HANA tools extension. Start the devspace.

Step 2:

Open terminal in the development space. Create an application by running the below command  in the directory : /home/user/projects
mvn -B archetype:generate -DarchetypeArtifactId=cds-services-archetype \
-DarchetypeVersion=RELEASE \ -DartifactId=temporal-cap-service

It will download required maven libraries and create a project.  Open created project in explorer.

File-> Open Workspace

Step 3:

Create the file schema.cds under db folder. Define two entities Product and Product_hist giving required field data.

Product entity is for original data where Product_hist is to capture the data which has been modified.

We can mark the fields validFrom and validTo with the annotations - @CDS.api.ignore and @assert.notNull in order to make sure these fields are not appeared in the metadata of entity’s odata and we don’t need to pass these fields in the payload while creating.
namespace rdp;

entity Product {
key productId : String(20);
productName : String(20);
productCategory : String(20);
productGroupId : String(20);
@assert.notNull: false
validFrom : Timestamp not null;
@assert.notNull: false
validTo : Timestamp not null;

entity Product_History {
productId : String(20);
productName : String(20);
productCategory : String(20);
productGroupId : String(20);
validFrom : Timestamp;
validTo : Timestamp;

Step 4:

Create a HANA artifact for systemversioning.


The history entity has essentially the same structure as the main entity but must also meet a number of consistency requirements, specifically: the history entity does not have the key.

We need to make sure that in System Versioned HANA artifact file, the database tables and the columns we refer are in Uppercase.

Step 5:

Create corresponding service for the entity.
using rdp from '../db/schema';

service TemporalCapService {
entity Product as projection on rdp.Product;


Deploy the code using MTA configurations giving HANA service instance details.

Once the code is deployed we could see that the  tables are created in HANA DB. And we can have OData services to perform CRUD operations as well.


CRUD Operations on temporal entities:


Create Request: POST

Request Payload:

Object is created with ValidFrom and ValidTo columns populated time details.

Update Request :PATCH'test1')

Request Payload:

We can see that, RDP_PRODUCT table is updated with new productname, where as RDP_PRODUCT_HISTORY has a record which was changed in the original table.

Delete Request:

Deletes the data from DB but the history table will get updated with the data present in the original table.'test1')


This is all from my side. I hope this article is useful. Thank you.