SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
⤢ Open full site

Create a Reusable Service

Create a service that will later on be reused in another CAP Java project.

Overview

🎓 beginner 20 min. SAP Cloud Application Programming ModelBeginnerSAP Business Technology PlatformJava

You will learn

  • How to write an entity definition
  • How to use some generic CAP artifacts like aspects and types
  • What associations and compositions are
  • How to make the project reusable The previous tutorial was about quickly setting up a working CAP application and read/write some mock data. This tutorial is about learning how to extend the application to a complete products service. You will take advantage of many of the out-of-the-box features provided by the CAP Java SDK such as using H2 as a database for local development. As a result, you will remove your custom handlers written in the first tutorial. In subsequent tutorials you will swap out H2 with the SAP HANA service, when preparing your application for the cloud.
René Jeglinsky R René Jeglinsky December 10, 2024
Created by December 10, 2024
Contributors

Prerequisites

Steps

Step 1 Define the domain model

In the previous tutorial, you defined a service, which defined its own entity. When modeling with CDS the best practice is to separate services from the domain model.

Therefore, you will now define the complete domain model that is used by the products service. The domain model is stored in the db folder of your CAP application.

  1. Go to your db folder and create a file called schema.cds.

    schema cds file creation
    schema cds file creation

  2. Add the following code to your newly created schema.cds file and make sure you Save the file:

    CDS
    namespace sap.capire.products;
    
    using { Currency, cuid, managed, sap.common.CodeList } from '@sap/cds/common';
    
    entity Products : cuid, managed {
        title    : localized String(111);
        descr    : localized String(1111);
        stock    : Integer;
        price    : Decimal(9,2);
        currency : Currency;
        category : Association to Categories;
    }
    
    entity Categories : CodeList {
        key ID   : Integer;
        parent   : Association to Categories;
        children : Composition of many Categories on children.parent = $self;
    }
Step 2 Understand keywords
+
Step 3 The `localized` keyword
+
Step 4 Associations and compositions
+
Step 5 The `cuid` and `managed` aspects
+
Step 6 The `CodeList` aspect and the `Currency` type
+
Step 7 Get more information about `@sap/cds/common`
+
Step 8 Rewrite the `AdminService`
+
Step 9 Use CAP's generic persistence handling
+
Step 10 Run and test your application
+
Step 11 Set up for reuse
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 11
1. Define the domain model 2. Understand keywords 3. The `localized` keyword 4. Associations and compositions 5. The `cuid` and `managed` aspects 6. The `CodeList` aspect and the `Currency` type 7. Get more information about `@sap/cds/common` 8. Rewrite the `AdminService` 9. Use CAP's generic persistence handling 10. Run and test your application 11. Set up for reuse

Learn more →