SAP ABAP CDS View Hierarchy

In SAP ABAP CDS (Core Data Services) Views, a View Hierarchy allows you to define relationships between CDS views, helping to model structured or hierarchical data efficiently. This is commonly used in scenarios such as organizational structures, product categories, or BOM (Bill of Materials).


Types of Hierarchies in CDS Views

  1. Self-Referencing Hierarchy (Parent-Child Relationship)
  2. Level-Based Hierarchy (Fixed Depth)
  3. Recursive Hierarchy (Unlimited Depth using Hierarchy Annotations)

1. Self-Referencing Hierarchy (Parent-Child Relationship)

A self-referencing hierarchy is when a table has a parent-child relationship within itself.

Example: Organizational Hierarchy

@AbapCatalog.sqlViewName: 'ZORG_HIER'
@EndUserText.label: 'Organization Hierarchy'
define view ZORG_HIER as select from zorg_table {
key org_id, // Organization Unit ID
org_name, // Name of the unit
parent_org_id // Parent Organization ID (Self-referencing)
}

🔹 Explanation:


2. Level-Based Hierarchy (Fixed Depth)

A level-based hierarchy is when each hierarchy level is explicitly defined. For example, a Product Category Hierarchy can have predefined levels (Category, Subcategory, Product).

Example: Product Hierarchy

@AbapCatalog.sqlViewName: 'ZPRODUCT_HIER'
@EndUserText.label: 'Product Hierarchy'
define view ZPRODUCT_HIER as select from zproduct_table {
key product_id,
product_name,
category_id, // Level 1
subcategory_id, // Level 2
type_id // Level 3
}

🔹 Usage:

🔹 Best for:


3. Recursive Hierarchy (Unlimited Depth)

SAP S/4HANA 1909+ introduced the Hierarchy Annotations, allowing recursive hierarchies inside CDS views. This is useful when the depth of hierarchy is unknown (e.g., BOM Structure).

Example: Employee Hierarchy (Recursive)

@AbapCatalog.sqlViewName: 'ZEMP_HIER'
@EndUserText.label: 'Employee Hierarchy'
@Hierarchy.parentChild:
[ child: emp_id, parent: manager_id, siblingsOrder: emp_name ]
define view ZEMP_HIER as select from zemployee_table {
key emp_id, // Employee ID
emp_name, // Employee Name
manager_id // Reports to (Parent-Child Relationship)
}

🔹 Explanation:

🔹 Best for:


Visualizing Hierarchy in Fiori Apps

SAP Fiori Elements support hierarchical CDS views, allowing navigation through tree-like structures.


Summary of CDS View Hierarchy Types

TypeUse CaseBest For
Self-ReferencingParent-Child RelationshipOrganization Structure, Reporting Line
Level-BasedFixed DepthProduct Hierarchy, Location Hierarchy
Recursive (Dynamic)Unlimited DepthBill of Materials (BOM), Complex Structures