Sub topic 13
ETags and Updatability Annotations
Assume you know SELECT, UPDATE, primary keys, and foreign keys. You also need Oracle Database 23ai or later and an existing JSON-relational duality view. This chapter explains how duality views prevent lost updates and how their annotations control which writes are allowed.
Why ETags Matter
An ETag is a value that identifies a particular version of a document. Oracle includes an ETag in the _metadata object returned by a duality view:
SELECT json_value(data, '$._id' RETURNING NUMBER) AS department_id,
json_value(data, '$._metadata.etag') AS etag
FROM department_dv;An application reads the document and saves its ETag. When it later sends an update, it includes the saved ETag. The update succeeds only if the document still has that version.
This is optimistic concurrency control. The database does not lock the document while the application edits it. Instead, the application checks for a conflicting change when it writes.
A typical SQL update uses the ETag in its search condition:
UPDATE department_dv
SET data = json_transform(
data,
SET '$.departmentName' = 'Applied Research'
)
WHERE json_value(data, '$._id' RETURNING NUMBER) = :department_id
AND json_value(data, '$._metadata.etag') = :expected_etag;The application should check the number of rows updated. One row means the ETag matched. Zero rows means that another transaction changed the document, so the application must reread the document and decide whether to merge or reject the change.
An ETag changes when mapped document data changes. It therefore detects changes made through the duality view and changes made directly to the underlying relational tables.
Controlling Writes with Annotations
A duality view maps JSON fields to columns and rows. Oracle must know which operations are safe for each mapped table. WITH annotations in the view definition provide that information.
CREATE JSON RELATIONAL DUALITY VIEW department_dv AS
SELECT JSON {
'_id' : d.department_id,
'departmentName' : d.name,
'employees' : [
SELECT JSON {
'employeeId' : e.employee_id,
'name' : e.name
}
FROM employee e WITH INSERT UPDATE
WHERE e.department_id = d.department_id
]
}
FROM department d WITH INSERT UPDATE DELETE;The annotations have these meanings:
| Annotation | Meaning |
|---|---|
| `WITH INSERT` | The operation may create a row in the mapped table. |
| `WITH UPDATE` | The operation may change mapped column values. |
| `WITH DELETE` | The operation may remove a mapped row. |
The parent department table permits all three operations. The child employee table permits inserts and updates, but not deletes. Consequently, deleting an employee through this view is not allowed, even though deleting a department is allowed.
Omitting an operation is a safety boundary. It prevents a document-shaped request from performing a relational operation that the view designer did not approve. For example, a view can expose a child collection as read-only by leaving write annotations off that table.
Annotations do not bypass database rules. Primary keys, foreign keys, NOT NULL constraints, check constraints, triggers, and privileges still apply. A view update must satisfy both the duality view's annotations and the underlying relational schema.
Choosing a Concurrency Strategy
Use ETag checks when multiple users or services can update the same document. If a conflict occurs, the application can show both versions, reload the document, or apply a domain-specific merge.
For a single controlled writer, an ETag check may be unnecessary, but keeping it makes the write behavior safer if another writer is added later.
Exercise
A client reads a department with ETag E1. Another transaction changes an employee in that department. The client then updates the department using E1. What should happen?
Show answer
The update should affect zero rows because the document's ETag changed. The client should reread the document and handle the conflict instead of overwriting the newer version.