Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Apache MetaModel offers three different ways of defining a query for a DataContext to retrieve data. Each approach offers a set of features, making them appropriate for different situations.

1 - The Query object

At the core of all query execution in MetaModel is the Query object. Query is a regular Java class containing the structural elements that make up a query. The main elements of a query is a set of clauses (SelectClause, FromClause and so on) which in turn each contain a set of items (SelectItem, FromItem etc.).

The object structure is depicted in the below class diagram. Each item will typically refer to some structure in the DataContext's schema model (a Table, a Column or such), or even an embedded sub-query. A query must have at least 1 SelectItem and 1 FromItem for it to be executable.

...

  • Creating the query this way is a bit verbose, and as we shall see below, it can be made simpler in most cases.

2 - The Query-builder API

To make query definition easier and more developer-guided, Apache MetaModel also has the so-called Query-builder API. This term is used for the set of methods extending from a call to DataContext.query(), ending with either .toQuery() or .execute(). Using the same simple example as before, we can define and execute the query like this:

...

  • Some features are not available via this API, for instance sub-querying.

3 - Query parsing

Finally, Apache MetaModel allows you to simply parse a query from a string form. You can use this feature in a two-step parse-then-execute mode or in a single step to parse-and-execute.

...