Composite DataContext

A composite DataContext is a DataContext that allows you to access several datastores as if they where one. It makes it easy to do queries that span several physical sources without having to deal with the complexity this usually involves.

A note of warning when dealing with composite DataContexts though: Queries will usually take up quite a lot of memory if you try to join tables from separate sources. Some may critisize the fact that MetaModel allows this somewhat "dangerous" behaviour but on the other hand we feel that any powerful tool can be used the wrong way and composite DataContexts are no exception.

Show me the code

Say you have two datastores, a CSV file and a database. Let's first create the composite DataContext from which we access them in a united fashion:

   1 DataContext csvDataContext = ...
   2 DataContext databaseDataContext = ...
   4 DataContext compositeDataContext = DataContextFactory.createCompositeDataContext(csvDataContext, databaseDataContext);

Because we might encounter name-clashes in table and column names, it is still possible to create our queries using the originial DataContext's schema structures. It is also possible to retrieve the schema structure from the composite.

   1 Table csvTable1 = csvDataContext.getDefaultSchema().getTableByName("customers");
   2 Table csvTable2 = compositeDataContext.getDefaultSchema().getTableByName("customers");
   4 // these should be the same!
   5 assert csvTable1 == csvTable2;

We can now create a query using structures from both of the contained DataContexts:

   1 Column csvNameColumn = csvTable1.getColumnByName("name");
   2 Table dbTable = databaseDataContext.getDefaultSchema().getTableByName("customers");
   4 Query q = compositeDataContext.query().from(csvTable1).leftJoin(dbTable)
   5            .on(csvNameColumn, dbTable.getColumnByName("name"))
   6            .select(csvNameColumn, dbTable.getColumnByName("id")).toQuery();

This query will correspond to (if it was possible to execute something like this in SQL):

FROM customers.csv.customers LEFT JOIN PUBLIC."CUSTOMERS"

And ... executing it will print out a list of customer names (from the CSV file) and their id's in the database. In our test suite we have an example of this, it prints out:

Diecast Collectables


Lego (not in DB)


Motor Mint Distributors Inc.





examples/CompositeDataContext (last edited 2013-08-01 09:56:27 by KasperSorensen)