I created an ABAP CDS hierarchy and I want to use it in another CDS to get the root node ID as a field. How can I do that?

This is my source table:

@EndUserText.label : 'Hierarchy'
@AbapCatalog.enhancement.category : #EXTENSIBLE_CHARACTER_NUMERIC
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table hierarchy_table {
  key mandt   : mandt not null;
  key id      : char10 not null;
  parent      : char10;
}

This is my hierarchy source CDS:

@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Hierarchy Source'
@Metadata.ignorePropagatedAnnotations: true
define view entity ZI_HierarchySource as select from hierarchy_table
  association [1..1] to ZI_HierarchySource as _hierarchy on $projection.parent = _hierarchy.id
{
      _hierarchy,
      key id,
      parent
}

This is my hierarchy CDS:

define hierarchy ZI_HIERARCHY
  as parent child hierarchy( source ZI_HierarchySource
  child to parent association _hierarchy
  siblings order by id ascending )
{
  id,
  parent,
  $node.node_id               as hiera_node_id,
  $node.parent_id             as hiera_parent_id,
  $node.hierarchy_is_orphan   as hiera_is_orphan,
  $node.hierarchy_is_cycle    as hiera_is_cycle,
  $node.hierarchy_rank        as hiera_rank,
  $node.hierarchy_parent_rank as hiera_parent_rank,
  $node.hierarchy_level       as hiera_level
}

And my end goal is a view :

define view entity ZI_NODE_ID_AND_ROOT_ID
  as select from hierarchy_table 
  association [1..1] to ZI_Hierarchy as hier on $projection.id = hier.id
{
  key id,
      root //somehow get it from the hierarchy?
}

In ABAP, I could use Hierarchy Node Navigator HIERARCHY_ANCESTORS, but I don’t know how to do it in another CDS.

Khám phá các thẻ bài đăng