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
- Self-Referencing Hierarchy (Parent-Child Relationship)
- Level-Based Hierarchy (Fixed Depth)
- 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:
org_id
represents an organizational unit.parent_org_id
points to anotherorg_id
, creating a hierarchical structure.
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:
category_id
→ Main Category (e.g., Electronics)subcategory_id
→ Subcategory (e.g., Mobile Phones)type_id
→ Specific Type (e.g., Smartphones)
🔹 Best for:
- Scenarios where the number of levels is fixed.
- Example: Country > State > City
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:
@Hierarchy.parentChild
defines the hierarchical structure in the CDS view.- child:
emp_id
(Employee) - parent:
manager_id
(Manager) - siblingsOrder: Defines sorting within the hierarchy
🔹 Best for:
- Bill of Materials (BOM)
- Reporting Structures (Manager-Employee Relationship)
Visualizing Hierarchy in Fiori Apps
SAP Fiori Elements support hierarchical CDS views, allowing navigation through tree-like structures.
Summary of CDS View Hierarchy Types
Type | Use Case | Best For |
---|---|---|
Self-Referencing | Parent-Child Relationship | Organization Structure, Reporting Line |
Level-Based | Fixed Depth | Product Hierarchy, Location Hierarchy |
Recursive (Dynamic) | Unlimited Depth | Bill of Materials (BOM), Complex Structures |