You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: java/migration.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -98,6 +98,7 @@ Some property defaults have been adjusted:
98
98
|`cds.security.authorization.instanceBased.rejectSelectedUnauthorizedEntity.enabled`| false | true | Requests that violate instance-based authorization conditions now fail with 403, instead of 404. |
99
99
|`cds.security.authorization.instanceBased.checkInputData.enabled`| false | true |[Authorization Checks On Input Data](./security#input-data-auth) are now enabled by default. |
100
100
|`cds.errors.defaultTranslations.enabled`| false | true |[Translations for Validation Error Messages](./event-handlers/indicating-errors#ootb-translated-messages) are now enabled by default. |
101
+
|`cds.sql.runtimeView.mode`| resolve | cte |[Runtime views](./working-with-cql/query-execution#runtimeviews) are now by default translated into Common Table Expressions |
Copy file name to clipboardExpand all lines: java/working-with-cql/query-execution.md
+42-22Lines changed: 42 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -400,11 +400,17 @@ The `lock()` method has an optional parameter `timeout` that indicates the maxim
400
400
401
401
The parameter `mode` allows to specify whether an `EXCLUSIVE` or a `SHARED` lock should be set.
402
402
403
-
## Runtime Views { #runtimeviews}
403
+
## Runtime Views { #runtimeviews}
404
404
405
-
The CDS compiler generates [SQL DDL](../../guides/databases?impl-variant=java#generating-sql-ddl) statements based on your CDS model, which include SQL views for all CDS [views and projections](../../cds/cdl#views-projections). This means adding or changing CDS views requires a deployment of the database schema changes.
405
+
The CDS compiler generates [SQL DDL](../../guides/databases?impl-variant=java#generating-sql-ddl) statements from your CDS model, including SQL views for all CDS [views and projections](../../cds/cdl#views-projections). As a result, adding or modifying CDS views typically requires redeploying the database schema.
406
406
407
-
To avoid schema updates due to adding or updating CDS views, annotate them with [@cds.persistence.skip](../../guides/databases#cds-persistence-skip). In this case the CDS compiler won't generate corresponding static database views. Instead, the CDS views are dynamically resolved by the CAP Java runtime.
407
+
To avoid schema redeployments when you add or update CDS views, annotate them with [@cds.persistence.skip](../../guides/databases#cds-persistence-skip). This annotation tells the CDS compiler to skip generating database views for these entities. Instead, the CAP Java runtime dynamically resolves such views at runtime.
408
+
409
+
::: warning Limitations
410
+
Runtime views support only simple [CDS projections](../../cds/cdl#as-projection-on). They do not support complex views that use aggregations, unions, joins, or subqueries in the `FROM` clause. To read [draft-enabled](../fiori-drafts#reading-drafts) entities, set `cds.drafts.persistence` to `split`. [Calculated elements](../../cds/cdl#calculated-elements) are not yet supported in runtime views.
411
+
:::
412
+
413
+
For example, consider the following CDS model and query:
408
414
409
415
```cds
410
416
entity Books {
@@ -413,33 +419,47 @@ entity Books {
413
419
stock : Integer;
414
420
author : Association to one Authors;
415
421
}
416
-
@cds.persistence.skip // [!code focus]
417
-
entity BooksWithLowStock as projection on Books { // [!code focus]
418
-
id, title, author.name as author // [!code focus]
419
-
} where stock < 10; // [!code focus]
422
+
@cds.persistence.skip
423
+
entity BooksWithLowStock as projection on Books {
424
+
id, title, author.name as author
425
+
} where stock < 10;
420
426
```
421
-
422
-
At runtime, CAP Java resolves queries against runtime views until an entity is reached that isn't annotated with *@cds.persistence.skip*. For example, the CQL query
423
-
424
427
```sql
425
428
Select BooksWithLowStock where author ='Kafka'
426
429
```
427
430
428
-
is executed against SQL databases as
429
-
430
-
```SQL
431
-
SELECTB.ID, B.TITLE, A.NAMEas"author"FROM BOOKS AS B
432
-
LEFT OUTER JOIN AUTHORS AS A ONB.AUTHOR_ID=A.ID
433
-
WHEREB.STOCK<10ANDA.NAME= ?
434
-
```
431
+
CAP Java provides two modes for resolving runtime views:
435
432
436
-
::: warning Limitations
437
-
Runtime views are supported for simple [CDS projections](../../cds/cdl#as-projection-on). Expands of [filtered associations](../../cds/cdl#publish-associations-with-filter) are only supported since `3.7.0`. Constant values and expressions in runtime views are supported since `3.8.0`.
433
+
**`cte` mode**: The runtime translates the view definition into a _Common Table Expression_ (CTE) and sends it with the query to the database.
438
434
439
-
Complex views using aggregations or union/join/subqueries in `FROM` are not supported and for reading [draft-enabled](../fiori-drafts#reading-drafts) entities, `cds.drafts.persistence` needs to be set to `split`.
435
+
```sql
436
+
WITH BOOKSWITHLOWSTOCK_CTE AS (
437
+
SELECTB.ID,
438
+
B.TITLE,
439
+
A.NAMEAS"AUTHOR"
440
+
FROM BOOKS B
441
+
LEFT OUTER JOIN AUTHOR A ONB.AUTHOR_ID=A.ID
442
+
WHEREB.STOCK<10
443
+
)
444
+
SELECT ID, TITLE, AUTHOR AS"author"
445
+
FROM BOOKSWITHLOWSTOCK_CTE
446
+
WHEREA.NAME= ?
447
+
```
448
+
449
+
::: tip
450
+
CAP Java 4.x uses `cte` mode by default. In 3.10, enable it with **cds.sql.runtimeView.mode: cte**.
440
451
:::
441
452
442
-
### Using I/O Streams in Queries
453
+
**`resolve` mode**: The runtime _resolves_ the view definition to the underlying persistence entities and executes the query directly against them.
454
+
455
+
```sql
456
+
SELECTB.ID, B.TITLE, A.NAMEAS"author"
457
+
FROM BOOKS AS B
458
+
LEFT OUTER JOIN AUTHORS AS A ONB.AUTHOR_ID=A.ID
459
+
WHEREB.STOCK<10ANDA.NAME= ?
460
+
```
461
+
462
+
## Using I/O Streams in Queries
443
463
444
464
As described in section [Predefined Types](../cds-data#predefined-types) it's possible to stream the data, if the element is annotated with `@Core.MediaType`. The following example demonstrates how to allocate the stream for element `coverImage`, pass it through the API to an underlying database and close the stream.
CAP Java doesn't have a dedicated API to execute native SQL Statements. However, when using Spring as application framework you can leverage Spring's features to execute native SQL statements. See [Execute SQL statements with Spring's JdbcTemplate](../cqn-services/persistence-services#jdbctemplate) for more details.
0 commit comments