Skip to content

Java: Views and Projections #1916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open

Java: Views and Projections #1916

wants to merge 24 commits into from

Conversation

MattSchur
Copy link
Contributor

@MattSchur MattSchur commented Jun 11, 2025

Introduces "Views and Projections" section with

  • Runtime Views
  • Write through Views

and documents some rules for write via path expressions in views.

Also shortens some headers to improve the readability in the navigation bar.

@MattSchur MattSchur requested a review from smahati as a code owner June 11, 2025 16:17
@MattSchur MattSchur changed the title Java: Write through Views Java: Views and Projections Jun 12, 2025
@MattSchur MattSchur requested a review from vmikhailenko June 12, 2025 15:03
@vmikhailenko
Copy link
Contributor

Overall, LGTM. It is nice that we explain this stuff.

@MattSchur MattSchur requested a review from vmikhailenko June 16, 2025 13:39
- [Path expressions](../../cds/cql#path-expressions) over compositions (*header.status*) are writable. For [Inserts](./query-api#insert), the view must expose all *not null* elements of the target entity and the data must include values for all of them. In the example above, the order header must have a generated key to support inserting new orders with a value for *headerStatus*.

::: warning Path Expressions over Associations
Path expressions navigating *associations* (*header.customer.name*) are [not writable](#cascading-over-associations) by default. To avoid issues on write, annotate them with [@readonly](../../guides/providing-services#readonly).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is explained earlier

For example, the following CDS model defines two resolvable views on the Order entity, which has a to-many items and a to-one header composition to a Header entity with a to-one customer association. In the projection, you can use path expressions as shown by headerStatus (writable) and customerName (read-only):

Why repeat it again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above it's only a side note and since @readonly should be added, a box makes it clearer

Do not use [*to-many associations*](../../cds/cdl#to-many-associations) in the select clause of CDS views. This blocks write operations and may cause performance issues due to record duplication on read.
:::
::: warning Avoid Composition Definitions in Views
Avoid [defining](../../cds/cql#association-definitions) new *compositions* in CDS views and prefer *associations* instead, as [deep write](#updatable-views) and [cascading delete](#delete-via-view) are only supported for compositions in persistence entities.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this true only for runtime views? I seen stakeholders who used such models and that surprisingly worked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This recommendation is for all views. On write there is not much of a difference between standard views and runtime views, as we need to resolve in both cases. Therefore the "write|delete through view" sections. Deep write/delete for such compositions needs to be implemented via custom logic.



::: tip Draft Queries on Runtime Views
If you define runtime views on [draft-enabled](../fiori-drafts#reading-drafts) entities and want to run draft specific queries on these views, set the *cds.drafts.persistence* configuration to `split`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be "if you draft-enable runtime view", no? When I read this I always think about introducing projection on already draft-enabled projection.

I my projection is a runtime view and I want to draft-enable it, I have to use split persistence.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@MattSchur MattSchur Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This describes the case of defining a runtime view on an already draft-enabled entity.

From my understanding split mode is required for both cases and as of now, draft-enabling runtime views does not make much sense due to the draft table enforcing schema updates.

@vmikhailenko
Copy link
Contributor

I like new structure a lot more! Apart from minors, I am OK with this.


#### Read in `CTE` mode { #rtview-cte }

This is the default mode in CAP Java `4.x`. The runtime translates the view definition into a _Common Table Expression_ (CTE) and sends it with the query to the database.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the new default! 😍

@MattSchur MattSchur requested a review from rjayasinghe June 23, 2025 14:53
@MattSchur MattSchur requested a review from vmikhailenko June 25, 2025 14:02
@MattSchur
Copy link
Contributor Author

@BraunMatthias are you OK with this view docu change?

@MattSchur MattSchur requested a review from agoerler June 27, 2025 15:21

### Write through Views { #updatable-views }

You can run [Insert](./query-api#insert), [Upsert](./query-api#upsert), and [Update](./query-api#update) statements on CDS views that are writable on the database or can be resolved to a single persistence entity by the CAP Java runtime.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can run [Insert](./query-api#insert), [Upsert](./query-api#upsert), and [Update](./query-api#update) statements on CDS views that are writable on the database or can be resolved to a single persistence entity by the CAP Java runtime.
You can run [Insert](./query-api#insert), [Upsert](./query-api#upsert) and [Update](./query-api#update) statements on CDS views that are writable on the database or can be resolved to a single persistence entity by the CAP Java runtime.


The CDS compiler generates [DDL](../../guides/databases?impl-variant=java#generating-sql-ddl) files from your CDS model, including SQL views for the CDS views. These views are deployed to the [database](../cqn-services/persistence-services#database-support) and used by the CAP runtime to read data.

For *read-only* views, you can use the full feature set of [selects](../../cds/cdl#as-select-from), including *joins*, *unions*, and *aggregations*. However, such complex views are not writable and require a schema redeployment if the view definition is changed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For *read-only* views, you can use the full feature set of [selects](../../cds/cdl#as-select-from), including *joins*, *unions*, and *aggregations*. However, such complex views are not writable and require a schema redeployment if the view definition is changed.
In addition, views that are used only for read access offer complex features to recombine data including *joins*, *unions*, and *aggregations*. However, be aware that such views enhanced with complex features **do not allow write queries and hence are restricted to read-only access**. Also such views always require schema redeployment if the view definition is changed. Read-only views support the full feature set of [selects](../../cds/cdl#as-select-from) to query data.
::: tip Prefer simple views
Prefer creating simple views, each tailored to a specific use case, rather than a single complex view that tries to address multiple use cases simultaneously.
:::


Views and projections can be resolved if the following conditions are met:
::: tip Prefer simple views
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.u.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants