Search

Query Result Processing

The result of a query is abstracted by the Result interface, which is an Iterable of Row. A Row is just a Map augmented with some convenience methods.

You can iterate over a Result:

Result result = ...

for (Row row : result) {
  System.out.println(row.get("title"));
}

Or process it with the Stream API:

Result result = ...

result.forEach(r -> System.out.println(r.get("title")));

result.stream().map(r -> r.get("title")).forEach(System.out::println);

If your query is expected to return exactly one row you can access it with the single method:

Result result = ...

Row row = result.single();

If it optionally returns a result, like a find by id would, you can obtain it using first:

Result result = ...

Optional<Row> row = result.first();
row.ifPresent(r -> System.out.println(r.get("title")));

The element names and their types are checked only at runtime. Alternatively you can use interfaces to get typed access to the result data:

interface Book {
  String getTitle();
  Integer getStock();
}

Row row = ...
Book book = row.as(Book.class);

String title = book.getTitle();
Integer stock = book.getStock();

Interfaces can also be used to get a typed list or stream over the result:

Result result = ...

result.listOf(Book.class);

result.streamOf(Book.class).map(Book::getTitle).forEach(System.out::println);

For the entities defined in the data model, CDS4j can generate interfaces for you through a Maven plugin.