Relational database tables combine into a JSON document for an AI assistant.

MySQL JSON Duality Views can turn related rows into a JSON document and write changes back to the underlying tables. I built a Java application to try that with an order, its customer, and its line items, then gave a local AI assistant read-only tools over the same data.

The reads were straightforward. The writes needed more care. Leaving an item out of the document deleted that order line. Leaving out the etag allowed a stale update to overwrite a newer cancellation. My API had to require both the complete document and the etag.

I also compared plain JDBC, JPA, and Spring Data JDBC, inspected the query plans, and tested what happened when the model misread an order’s status. Clean JSON simplified the data access, but business rules and customer access checks still belonged in Java.

Read the full article on Foojay: One Database, Two Models.

I used an AI assistant to help build the example, run the tests and edit this article. The code, results and traces are all in the companion repository.