> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Adding a hierarchy

This page provides a description of how you can expose a field as a hierarchy. It features an example in which we expose a previously added field 'TestField' as a hierarchy. For a full walk-through on how this was done, see [Adding a Field](./adding-a-field). The techniques employed are generic examples that can be extended, adapted and repeated for any use case that we need.

To avoid forking library code, all extensions are made by subclassing the relevant configuration class from the `cvarc-starter`, `cvarc-ba-config` or `cvarc-sa-config` library modules and wiring the subclass into your project's `ApplicationConfig` in the `cvarc-application` module.

## Step 1 - extending the cube dimension config

To expose a field as a hierarchy, create a project-specific subclass of the dimension config rather than editing the library class directly. In `cvarc-application/src/main/java/com/activeviam/cvarc/application/cfg/extensions/`, create:

* `CustomBACubeDimensionConfig` extending `BACubeDimensionConfig` (for the BA cube)
* `CustomSACubeDimensionsConfig` extending `SACubeDimensionsConfig` (for the SA cube)

Annotate the subclass with `@Configuration` and `@Primary`, override the `dimensions(...)` method, call `super.dimensions(builder)`, and append the new hierarchy.

<Note>
  **Prerequisite information**

  To learn more about navigating the managerDesc, see [Navigating managerDesc](./navigating-managerdesc).
</Note>

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
@Configuration
@Primary
public class CustomBACubeDimensionConfig extends BACubeDimensionConfig {

    @Override
    public ICanBuildCubeDescription<IActivePivotInstanceDescription> dimensions(
            ICanStartBuildingDimensions builder) {
        return super.dimensions(builder)
                .withDimension("TestField")
                .withSingleLevelHierarchy("TestField");
    }
}
```

Then add the new class to the `@Import` list of `ApplicationConfig` in `cvarc-application/src/main/java/com/activeviam/cvarc/application/cfg/ApplicationConfig.java`.

## Step 2 - extending the cube's selection

The selection for each cube is exposed on the bean via `schemaSelectionDescription(IDatastoreSchemaDescription)` in `BACubeConfig` / `SACubeConfig` (in the `cvarc-starter` library module). That method delegates to the `public static` helpers `createBASelection(...)` / `createSASchemaSelectionDescription(...)`, which build the selection chain. Static helpers cannot be overridden, so the extension overrides the non-static `schemaSelectionDescription` method and rebuilds the selection — selection descriptions are immutable, so post-processing the parent's result is not viable.

In `cvarc-application/src/main/java/com/activeviam/cvarc/application/cfg/extensions/`, create a `CustomBACubeConfig extends BACubeConfig` (or `CustomSACubeConfig extends SACubeConfig`), annotated with `@Configuration` and `@Primary`, and override `schemaSelectionDescription` to mirror the parent's selection and append the reference to your new field's store.

<Note>
  Make sure that any new field added to the selection does not exist in the selection already, and that it points to a corresponding field in a datastore.
</Note>

```java theme={"languages":{"custom":["/engine/python-sdk/0.9/languages/pycon.tmLanguage.json"]}}
@Configuration
@Primary
public class CustomBACubeConfig extends BACubeConfig {

    @Override
    public ISelectionDescription schemaSelectionDescription(IDatastoreSchemaDescription dsSchemaDesc) {
        // Mirror BACubeConfig.createBASelection(dsSchemaDesc) and append the TestField reference.
        return StartBuilding.selection(dsSchemaDesc.asDatabaseSchema())
                .fromBaseStore(BAStoreNames.BA_BASE_STORE_NAME)
                .withAllFields()

                // BA_BASE_TO_TRADE_DESCRIPTION_REF
                .usingReference(BAStoreNames.BA_BASE_TO_TRADE_DESCRIPTION_REF)
                .withAllFields()
                .except(BAStoreNames.TRADE_DESCRIPTION_AS_OF_DATE,
                        BAStoreNames.TRADE_DESCRIPTION_TRADE_ID,
                        BAStoreNames.TRADE_DESCRIPTION_REFERENCE_NAME)

                // ...

                // Adding the TestField to selection
                .usingReference("BaseStore to TestFieldStore") // Reference from the Base Store to our new field's store
                .withAllFields()
                .except("AsOfDate", "TradeId") // Exclude fields that are found in other stores. The application will not start if the selection has duplicate fields unintentionally.
                .build();
    }
}
```

Then add `CustomBACubeConfig` (or `CustomSACubeConfig`) to the `@Import` list of `ApplicationConfig`. The same pattern applies to the SA cube — override `schemaSelectionDescription` on `CustomSACubeConfig extends SACubeConfig`.
