Skip to content

Commit a7bea18

Browse files
niemdabeckermarcsmahati
authored
Java: Explain deep updates supported by change tracking with examples (#1963)
Attempts to illustrate supported/non-supported statements in change tracking. --------- Co-authored-by: Marc Becker <marc.becker@sap.com> Co-authored-by: Mahati Shankar <m.shankar@sap.com>
1 parent 65aeae7 commit a7bea18

1 file changed

Lines changed: 58 additions & 3 deletions

File tree

java/change-tracking.md

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,70 @@ values of the entity: data that weren't present in the old values are considered
283283
the new values are considered as deleted. Elements that are present in both old and new values but have different values
284284
are considered as modified. Each change detected by the change tracking feature is stored in the change log as a separate entry.
285285

286+
As a rule, specify primary keys to modify change tracked entities and avoid using [searched updates](/java/working-with-cql/query-api#searched-update).
287+
288+
### Changes in Deeply Structured Documents
289+
286290
In the case of the deeply structured documents, for example, entities with the compositions, the change tracking feature detects
287291
the changes across the complete document and stores them in the change log with the metadata reflecting the structure of the change.
288292

289-
Take the order and item model used previously in this guide as an example. If you change values for the tracked elements with the deep update, for example, the customer name in the order and the quantity of the item, the change log contains two entries: one for the order and one for the item. The change log entry for the item will also reflect that the root of the change is an order. Both changes will be reachable through the association `changes` of the order entity.
293+
Take the order and item model used previously in this guide as an example.
294+
295+
For deep updates, use the [delta representation](/java/working-with-cql/query-api#deep-update-delta) for its items or the [full set representation](/java/working-with-cql/query-api#deep-update-full-set) to overwrite complete document.
296+
297+
The following example yields two changelog entries: one for the order and one for the item. The change log entry for the item reflects that the root of the change is an order. Both changes are reachable through the association `changes` of the order entity.
298+
299+
```java
300+
Orders order = Orders.create("...");
301+
order.setOrderNo("N1");
302+
OrderItems item = OrderItems.create("...");
303+
item.setQuantity(3);
304+
305+
order.setItems(CdsList.delta(item));
306+
Update.entity(Orders_.class).entry(order);
307+
```
308+
309+
If a direct update is unavoidable, Specify the order item that needs to be updated with a path expression:
290310

291-
:::warning Prefer deep updates for change-tracked entities
292-
If you change the values of the `OrderItems` entity directly via an OData request or a CQL statement, the change log contains only one entry for the item. The change won't be associated with an order and will not be reachable through the `changes` association. While the updates of composition targets directly are possible, the change tracking feature does not attempt to resolve the parent entity by itself, it requires that either OData request or a CQL statement provide the reference to the parent, for example, `Orders`.
311+
```java
312+
OrderItems item = OrderItems.create("...");
313+
item.setQuantity(3);
314+
Update.entity(Orders_.class, o -> o.filter(f -> f.ID().eq("...")).items()).entry(item);
315+
```
316+
317+
Similarly, a `Delete` statement can be used to remove an item from an order:
318+
319+
```java
320+
Delete.from(Orders_.class, o -> o.filter(f -> f.ID().eq("...")).items().filter(i -> i.ID().eq("...")));
321+
```
322+
323+
The last segment can omit keys to indicate **bulk deletion**:
324+
325+
```java
326+
Delete.from(Orders_.class, o -> o.filter(f -> f.ID().eq("...")).items());
327+
```
328+
329+
The same path expression can be used in the `Insert` statement to directly add an item to a composition.
330+
331+
Changes are correctly referenced to the root if the following conditions are true:
332+
- The path expression starts at the root of the document (`Orders`, in this case).
333+
- The path navigates only through the compositions within the same document tree.
334+
- All segments of the path, except the last one, must include keys.
335+
336+
:::warning Limitation
337+
Avoid Direct modifications of composition items, they aren't supported by change tracking.
293338
:::
294339

340+
In the following example, the item's updated changelog entry _won't_ be associated with an order:
341+
342+
```java
343+
OrderItems item = OrderItems.create("...");
344+
item.setQuantity(3);
345+
Update.entity(OrderItems_.class).entry(item);
346+
```
347+
348+
You must rewrite such statements using one of the previously illustrated ways.
349+
295350
## Reacting on Changes
296351

297352
You can write an event handler to observe the change log entries. Keep in mind that the change log entries

0 commit comments

Comments
 (0)