How Associations Are Used in ABAP Programming

3 hours ago 1

In ABAP, associations define how business entities relate to one another and enable navigation, lifecycle control, and UI integration in SAP Fiori applications.

In this post, you’ll learn how to create and link associated entities—using “Ingredient” as an example—and connect them to the “Recipe” root entity through compositions, projections, behavior definitions, metadata extensions, and service definitions.

Step 1: Creating a CDS Data Definition for an Associated Entity

First, you must create a base entity for the associated entity. To do so, follow these steps:

  1. Open the context menu of the Data Definitions folder, and select the New Data Definition item. 

 “New Data Definition”

  1. In the dialog that opens, enter a name and description for the data definition. For our example, enter “ZACB_R_INGREDIENT” as the name. In the Referenced Object field, you must also enter a referenced object; in our example, this is the database table for the Ingredient object. Then, click the Next button.

Creating the Data Definition

  1. In the subsequent step, select a transport request and confirm this by clicking the Next button.
  2. Select a template. In our case, we want to create our own entity and therefore select defineViewEntity as the template. This template generates the most important fields for the data definition.

Selecting a Template for the Data Definition

  1. Confirm the selection of the template by clicking the Finish button.

The CDS coding will then be generated from the listing below based on the template and the referenced object.

define view entity ZACB_R_Ingredient select from zacb_ingredient

{

   key recipe_id as RecipeId,

   key ingredient_id as IngredientId,

   name as Name,

   quantity as Quantity,

   unit as Unit,

   created_by as CreatedBy,

   created_at as CreatedAt,

   local_last_changed_by as LocalLastChangedBy,

   local_last_changed_at as LocalLastChangedAt,

   last_changed_at as LastChangedAt,

   last_changed_by as LastChangedBy

}

Step 2: Creating a Projection View in ABAP CDS

Now you need to repeat steps 1–4 to create a projection entity. However, this time you don’t select a template (see figure below), but click directly on the Finish button.

Don’t Select a Template for the Projection Entity

An empty data definition gets created. You can use the defineProjection- View pattern to trigger an automatic code generation. For this purpose, you should enter “defineProje...” in the empty editor window. The quick fix function of the ABAP development tools will then suggest the appropriate template.

Selecting a Code Template for a Projection View

When you select the template, the code will automatically be generated from this listing.

@AccessControl.authorizationCheck: #NOT_REQUIRED

@EndUserText.label: 'Ingredient'

@Metadata.ignorePropagatedAnnotations: true

define view entity ZACB_C_Ingredient as projection on

   data_source_name

{

}

Step 3: Defining Fields and Keys in the Projection Entity

Add the name of the base entity here as data_source_name. You can also specify the fields of the base entity and use key to define which fields are key fields.

define view entity ZACB_C_Ingredient

   as projection on ZACB_R_Ingredient as Ingredient

{

   key RecipeId,

   key IngredientId,

      Name,

      Quantity,

      Unit,

      LocalLastChangedAt,

      LocalLastChangedBy,

      LastChangedAt,

      LastChangedBy,

}

Step 4: Linking Base Entities with Composition and Association

Now, the two base entities must first be linked to each other via an association. To do this, we first create a composition from the root entity in the direction of the linked entity (see listing below). A composition defines an existential dependency between two entities, whereby the child entity can’t exist without the parent entity.

define root view entity ZACB_R_Recipe

   as select from zacb_recipe

   composition [0..*] of ZACB_R_Ingredient as _Ingredient

   {

      key recipe_id    as RecipeId,

         recipe_name   as RecipeName,

At the level of the associated entity, you must use association to parent to add an association to the root entity. This creates an upward relationship with the root entity.

@EndUserText.label: 'CDS entity Ingredient'

define view entity ZACB_R_Ingredient

   as select from zacb_ingredient

   association to parent ZACB_R_Recipe as _Recipe

      on $projection.RecipeId = _Recipe.RecipeID

{

   key recipe_id as RecipeId,

   key ingredient_id as IngredientId,

Step 5: Connecting Projection Entities Using Redirected Associations

A link between the two entities must also be created at the level of the projection entities. In the root entity, you must add the redirected to composition child expression.

define root view entity ZACB_C_Recipe

   provider contract transactional_query

   as projection on ZACB_R_Recipe

{

   …

         _Ingredient : redirected to composition child

            ZACB_C_Ingredient,

}

A similar link is made from the associated entity to the root entity. Here, you need to enter the redirected to parent addition (see listing below). The relationship defined in this way is used to navigate between the entities in the service binding.

define view entity ZACB_C_Ingredient

   as projection on ZACB_R_Ingredient as Ingredient

{

         _Recipe : redirected to parent ZACB_C_Recipe

}

Step 6: Extending the Behavior Definition for Associated Entities

You must define a behavior for each entity. To do this, you want to extend the behavior definition as shown in this listing.

  • Associations are published in the entities.
  • Data records of associated entities such as the ingredient are created via the root entity.
  • The behavior of locks and authorizations takes place via the root entity.

managed implementation in class zbp_acb_r_recipe unique;

strict ( 2 );

with draft;

define behavior for ZACB_R_Recipe alias Recipe

   association _Ingredient { create; with draft; }

define behavior for ZACB_R_Ingredient alias Ingredient

persistent table zacb_ingredient

lock dependent by _Recipe

authorization dependent by _Recipe

draft table zacb_ingredien_d

{

   update;

   delete;

   field ( readonly ) RecipeId;

   field ( readonly ) IngredientId;

   association _Recipe { with draft; }

   mapping for zacb_ingredient

      {

      …

      }

}

These associations must also be defined in the behavior definition of the projection entities.

projection;

strict ( 2 );

use draft;

define behavior for ZACB_C_Recipe alias Recipe

   use association _Ingredient { create; with draft; }

define behavior for ZACB_C_Ingredient alias Ingredients

   use association _Recipe { with draft; }

Step 7: Updating Metadata Extensions for SAP Fiori Display

To ensure that the linked entities are displayed in the SAP Fiori app, both the metadata extension of the root entity must be extended and a separate metadata extension must be created for the child entities. A facet must be added to the metadata extension of the root entity.

{

id           : 'controlSection',

type         : #LINEITEM_REFERENCE,

position     : 20,

targetElement: '_Ingredient'

},

The targetElement annotation is used to indicate that the metadata extension of the associated entity is supposed to be called and displayed in this section.

Step 8: Exposing Associations in the Service Definition

Finally, the association must be specified in the service definition.

@EndUserText.label: 'Recipe'

define service ZACB_UI_RECIPE {

   expose ZACB_C_Recipe as Recipe;

   expose ZACB_C_Ingredient as Ingredient;

}

Once all created and modified development artifacts have been successfully activated, the link between the two entities can be seen in the service binding.

Service Binding with Associations

Repeat the steps shown here for all associated entities.

Conclusion

Associations in ABAP do more than connect entities—they define ownership, lifecycle dependency, authorization handling, and UI navigation across your application. By combining compositions, projection redirects, behavior definitions, metadata extensions, and service exposure, you create a fully navigable and transactional relationship between entities such as Recipe and Ingredient. Once activated, these associations seamlessly power SAP Fiori navigation and draft handling, making structured entity modeling a critical skill for modern ABAP development.

Editor’s note: This post has been adapted from a section of the book ABAP Cookbook: Practical Recipes for Modern Programming by Fabian Lupa and Sven Treutler. Fabian is a senior software engineer and trainer at Adesso, where he has worked since 2022. His primary responsibility is employee training and development in the context of ABAP programming, as well as developer enablement in customer projects. Sven is a passionate ABAP developer. He has worked on ABAP development at rku.it GmbH in Herne since 2010. There, he focuses on new technologies and quality assurance in the ABAP environment.

This post was originally published 2/2026.

Read Entire Article