Skip to Content

Develop a CAP JAVA App Using SAP Business Application Studio

Develop a simple CAP Java application using SAP Business Application Studio.
You will learn
  • How to create a CAP project
  • How to develop business applications based on the SAP Cloud Programming Model (CAP)
  • How to run and test your application using the Run Configurations tool
PaolauferPaola LauferMarch 17, 2025
Created by
Paolaufer
March 17, 2025
Contributors
Paolaufer

Prerequisites

The application you’ll develop is a simple bookshop app consisting of a data model with three entities:

  • Books
  • Authors
  • Genres

The data model is exposed via the Catalog Service. The application has some initial data that is used for testing the application, and some custom logic that runs after reading the books from the Books entity.

Once you have all the code in place, you will test the application locally.

  • Step 1
    1. From the SAP Business Application Studio hamburger menu, select File > New Project from Template.

      You can also go to the Command Palette and choose SAP Business Application Studio: New Project From Template.

    2. Select the CAP Project template, and click Start.

      start project
    3. Enter bookshop as the name for the project.

    4. From the Select your runtime dropdown list, select Java.

    5. Select the SAP HANA Cloud checkbox.

      hana
    6. Click Finish.

      The project is generated.

    7. From the SAP Business Application Studio hamburger menu, select File > Open Folder.

    8. In the command palette, open the projects folder.

      storyboard
    9. Select your project and click OK.

      storyboard

      The Storyboard is displayed.

      storyboard
  • Step 2
    1. In the Data Models tile, click + to create a data model entity.

      data model
    2. Click Create.

      create entity

      It may take a few moments for the Graphical Modeler to be populated.

    3. Change the entity name to Books.

      entity name
    4. Click on the entity and then click on the

      show details
      (Show Details) icon.

      show details
    5. In the Properties pane, click + to add new properties.

      show details
    6. Add the following properties:

      NameType
      title String
      descr String
      stock Integer
      price Decimal

      The Books entity should look like this:

      show details
    7. Click Add Entity.

      show details
    8. Rename the new entity Authors.

    9. In the Properties pane, click + to add a new property.

      show details
    10. Add the following property:

      NameType
      name String
    11. Click Add Entity.

      show details
    12. Rename the new entity Genres.

    13. Click on the Authors entity and then click on the Add Relationship icon.

      show details
    14. Drag the arrow to the Books entity.

      show details
    15. In the Relationship Details dialog, select the To-One radio button for the Cardinality and click OK.

      show details
    16. Click on the Genres entity and then click on the Add Relationship icon.

      show details
    17. Drag the arrow to the Books entity.

      show details
    18. In the Relationship Details dialog, select the Composition radio button for the Type.

    19. Select the To-Many radio button for the Cardinality and click OK.

      show details
  • Step 3
    1. In the storyboard, go to the Service tile, click + to create a service entity.

    2. Click Create.

      create entity
    3. Click on the service and select Add Service Entity.

      create entity
    4. From the Projection dropdown list, select Bookshop.Books.

    5. Click

      create entity
      to save your changes.

      create entity
    6. In the storyboard, click on the service and select Add Action/Function.

      create entity
    7. Change the name of the action to submitOrder.

      create entity
    8. In the Properties pane, add the following:

      Parameter NameParameter Type
      amount Integer
      books_id Integer
      create entity
  • Step 4
    1. In the storyboard, go to the Data Models tile, click on Authors, and select Add Data.

      create entity
    2. In the Data Editor, enter 3 for the number of rows with mock data and click Add.

      create entity
    3. Repeat the procedure to add rows for the Genres and the Books entities.

    4. In the command palette, search for the application.yaml file.

    create entity
    1. Add the following line in the cds section for the default profile.

      Java
      Copy
      data-source.csv.paths : "test/data/**"
      
      
    2. Save your changes.

      The file should look like this:

      create entity

      This configuration tells the application where the sample data is located within the project structure.

  • Step 5
    1. Go to the Explorer.

    2. Navigate to srv > src > main > java / customer / bookshop, and create a new folder called handlers.

      package for Custom Event Handlers
    3. Add a new file in the handlers folder called BookshopServiceHandler.java.

      package for Custom Event Handlers
    4. Populate the BookshopServiceHandler.java file with the following:

      Java
      Copy
      package customer.bookshop.handlers;
      import org.springframework.stereotype.Component;
      
      import com.sap.cds.Result;
      import com.sap.cds.services.cds.CdsReadEventContext;
      import com.sap.cds.services.cds.CqnService;
      import com.sap.cds.services.handler.EventHandler;
      
      import com.sap.cds.services.handler.annotations.After;
      import com.sap.cds.services.handler.annotations.ServiceName;
      
      @Component
      @ServiceName("bookshopService")
      public class BookshopServiceHandler implements EventHandler {
      
      @After(event = CqnService.EVENT_READ, entity = "bookshopService.Books")
      public void onRead(CdsReadEventContext context){
      Result result = context.getResult();
      //result.forEach(r -> System.out.println(r.get("title")));
      result.forEach(r -> {
      if( ((Integer) r.get("stock"))> 111)
      r.put("title",((String)r.get("title")).concat(" -- Discount"));
      });
      }
      }
      
      

      To learn more about working with event handlers, see Event Handlers.

    5. Save your changes.

      Your application should look similar to the structure shown in the picture below.

      Project structure

      You can also see the semantic structure of the application in the Project Overview.

      Project structure

    What is the Project Overview?

  • Step 6

    You can explicitly deploy your application to a persistent local SQLite database, or you can run your application and it will implicitly use an in-memory database.

    This step describes how to run the application with an in-memory database.

    You will first add all required dependencies, and then create and run a run configuration.

    1. Add and install all required dependencies.

      • From the Terminal menu, select New Terminal.

      • On the bookshop folder, run the following:

        NPM
        Copy
        npm install
        
        
    2. From the Activity pane, open the Run Configurations view.

      Open Run Configurations view
    3. Click + at the top of the view to add a new configuration.

      Open Run Configurations view
    4. Select Bookshop - (CAP Java) as the runnable application from the command palette prompt.

      Open Run Configurations view

      There might be other run configuration options available in the command palette.

    5. Press Enter to use the default name for the configuration. A new configuration is added to the run configuration tree.

    6. In the Configuration editor, select the Default Profile radio button for the database type.

      Open Run Configurations view
    7. Click the green arrow on the right of the configuration name to run the application.

      Run
    8. If prompted, open the application in a new tab.

      Run

      The application opens in the browser.

    9. Click on Books to see the metadata and entities for the service.

      Open Run Configurations view

      You can also debug your application to check your code logic. For example, to debug the custom logic for this application, perform the following steps:

    10. Place a breakpoint in the function in the service.js file.

    11. In the running app, click the Books entity. It should stop at the breakpoint.

      Breakpoint
    12. Click Continue in the debugger until all the books are read and the page is presented.

      Stop the app
    13. Remove the breakpoint.

    14. Stop the application by clicking Stop in the Debugger. The number beside the Debug icon represents the number of running processes. Click Stop until there are no processes running.

  • Step 7

    Prerequisites

    • Make sure you have an SAP HANA database available in your space. See Create an SAP HANA Database Instance Using SAP HANA Cloud Central

    • You have an SAP HANA service (SAP HANA as a Service or SAP HANA Cloud) available in your space.
      To create a new instance in your trial account:

      1. Go to your space in the SAP BTP cockpit.
      2. Go to Service Marketplace.
      3. Select SAP HANA Schemas & HDI Containers.
      4. Click Create.

        Create instance
      5. From the Plan dropdown menu, select hdi-shared as the service plan and provide a name for the new instance.
      6. Click Create.
    1. From the Activity pane, open the Run Configurations view.

      Open Run Configurations view
    2. Click + at the top of the view to add a new configuration.

      Open Run Configurations view
    3. Select Bookshop - (CAP Java) as the runnable application from the command palette prompt.

      Open Run Configurations view

      There might be other run configuration options available in the command palette.

    4. Press Enter to use the default name for the configuration. A new configuration is added to the run configuration tree.

    5. In the Configuration editor, select the SAP HANA Cloud radio button for the database type.

      Open Run Configurations view
    6. Log in to Cloud Foundry.

    7. From the SAP Cloud Instance dropdown list, select the relevant instance.

    8. From the Deploy the data model before running dropdown list, select the Deploy (with data) radio button.

      Open Run Configurations view
    9. Click the green arrow on the right of the run configuration.

      Open Run Configurations view
    10. If prompted, open the application in a new tab.

      Run

      The application opens in the browser.

    11. From the terminal on the bookshop folder, run cds add mta. This adds an mta.yaml file to the root of your application.

      Note: If you are working on a trial account, open the mta.yaml file, and in the resources section change the service parameter to hana. Save your changes.

    12. Right-click the mta.yaml file and choose Build MTA Project.

      Build MTA

      A new folder for mta_archives is created containing the new mtar file.

    13. Right-click the mtar file and choose Deploy MTA Archive.

      Deploy MTA

    Once the task is complete, your application should be available in your Cloud Foundry space.
    To access your application, go to your space in the SAP Cloud Platform cockpit and select Applications from the side menu.


Back to top