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/working-with-cql/query-api.md
+110-3Lines changed: 110 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -131,7 +131,6 @@ In the [CDS Query Language (CQL)](/cds/cql) builder, the lambda expression `o ->
131
131
To target components of a structured document, we recommend using path expressions with infix filters.
132
132
:::
133
133
134
-
135
134
### Filters {#target-entity-filters}
136
135
137
136
Besides using infix filters in path expressions, the `Select`, `Update`, and `Delete` builders support filtering the [target entity set](#target-entity-sets) via the `where` method. Using `where` is equivalent to defining an infix filter on the last segment of a path expression in the statement's `from` / `entity` clause. For statements that have both, an infix filter on the last path segment and a `where` filter, the resulting target filter is the conjunction (`and`) of the infix filter and the `where` filter.
@@ -1309,7 +1308,26 @@ The Query Builder API supports using expressions in many places. Expressions con
1309
1308
1310
1309
### Entity References {#entity-refs}
1311
1310
1312
-
Entity references specify entity sets. They can be used to define the target entity set of a [CQL](../../cds/cql) statement. They can either be defined inline using lambda expressions in the Query Builder (see [Target Entity Sets](#target-entity-sets)) or via the `CQL.entity` method, which is available in an _untyped_ version as well as in a _typed_ version that uses the generated [model interfaces](../cqn-services/persistence-services#model-interfaces). The following example shows an entity reference describing the set of *authors* that have published books in the year 2020:
1311
+
Entity references specify entity sets. They can be used to define the target entity set of a [CQL](../../cds/cql) statement or be an argument of event handler.
1312
+
1313
+
You can also get [entity references](query-execution#entity-refs) from the result of a CDS QL statement to address an entity via its key values in other statements.
1314
+
1315
+
Each reference has ordered sequence of _segments_ that define the path from the entity's root to the certain part of it. Segment has the _identifier_ with the name of the entity or an element and optional filter _predicate_.
1316
+
1317
+
Existing reference can be reused as an object or a variable, or a new reference can be built on top of it. References are not bound to the particular model and are not checked against it while they are being built.
1318
+
1319
+
References can be _absolute_ or _relative_. Absolute reference has fully qualified entity name as the identifier in the first segment. They usually have associations as their segments.
1320
+
1321
+
You start with the reference pointing to a book with certain key. You build it using corresponding [model interfaces](../cqn-services/persistence-services#model-interfaces) providing you the methods that corresponds to the elements of the book.
You can also get [entity references](query-execution#entity-refs) from the result of a CDS QL statement to address an entity via its key values in other statements.
1346
+
If you want to use this ref to fetch the author of this book, you use `CQL.entity(...)` to make it typed again and add one more segment to it. Note, that this does not check that original ref is indeed the ref to the book, this only lets you use the required model interface.
The result of both is the same reference. Model interfaces are strongly recommended for custom handlers as they are easier to use.
1359
+
1360
+
If you need to navigate to the parent, you can do it using the dynamic syntax to strip the last segment of it. This also accounts for the case when the reference contains longer path.
Filters that you define for references can contain complex predicates referring to other elements, for example, to express complex conditions and path expressions.
1394
+
1395
+
Segments of the references also usable as the variables and you can inspect them.
1396
+
1397
+
```java
1398
+
r.rootSegment().id(); // yields Books
1399
+
r.rootSegment().filter(); // filter an Optional with the predicate
1400
+
```
1401
+
1402
+
If you want to reflect the types behind the ref, you need to use [`CqnAnalyzer`](/java/working-with-cql/query-introspection#cqnanalyzer) that parses it and binds it back to the model. Use it, for example, to find annotations or extract key values.
1403
+
1404
+
References are not comparable so they cannot be used as a keys in maps and values in the sets. You should not compare references between each other.
1405
+
1406
+
Each reference can be serialized as JSON and also renders JSON as its `toString()` implementation. Do not use this JSON to process the references, for example, to extract values from them or to compare references between each other.
1407
+
1408
+
### Elements References {#element-refs}
1409
+
1410
+
Element reference points to an element of the entity. Such references are usually _relative_, they do not have the name of the entity in their root. They also can include filters in their segments except _the last one_.
1411
+
Most of the time, they exist as members of the [select list](#projections) of a statement or part of the statement, for example, of an expand predicate.
1412
+
1413
+
The following example illustrates the difference:
1414
+
1415
+
```java
1416
+
CqnSelect statement =Select.from(Books_.class, b -> b.filter(f ->f.ID().eq("...")))
They also share the same features and limitations as the entity references and they cannot be used with [`CqnAnalyzer`](/java/working-with-cql/query-introspection#cqnanalyzer).
0 commit comments