Introduction to MetaModel 3
The other day we released version 3 of MetaModel, a project that I have thoroughly enjoyed working on lately. Let me share a bit of my enthusiasm and try to convince you that this is the greatest data access library there is for Java.First let me also say, just to make it clear: MetaModel is NOT an ORM (Object-Relational Mapping) framework. MetaModel does not doing any mapping to your domain object model. Contrary, MetaModel is oriented towards working with the data model that already exists in your datastores (databases, files etc.) as it is physically represented. So the model of MetaModel is in deed a meta model, just as it is a metadata model - it works with the concepts of tables, columns, rows, schemas, relationships etc. But it is also oriented towards abstracting away all the cruft of having to deal with the physical interactions with each individual data storage technology. So unlike most ORMs, MetaModel allows you to work with arbitrary data models, stored in arbitrary technologies such as relational (JDBC) databases, text file formats, Excel spreadsheets, NoSQL databases (currently CouchDB and MongoDB) and more.
Here's an overview of the scope of MetaModel, depicted in our "module diagram".
Code examples
DataContext dc = DataContextFactory.createExcelDataContext(new File("people.xlsx"));
// getting column by path
Column customerNameColumn = dc.getColumnByQualifiedLabel("customers.name");
// traversing all schemas, tables, columns Schema schema = dc.getDefaultSchema(); Table[] tables = schema.getTables(); Column[] columns = tables[0].getColumns();
// step-wise getting specific elements based on names
Table customersTable = schema.getTableByName("customers");
Column customerBalanceColumn = customerTable.getColumnByName("balance");
Queries
// Simple query: Get all customer fields for customers with a credit balance greater than 10000. DataSet ds = dc.query().from(customersTable).select(customersTable.getColumns()) .where(customerBalanceColumn).greaterThan(10000).execute();
// Slightly more advanced query: Join customers with their associated sales representatives
// and group the result to count which sales reps have the most customers
Column salesRepId = customersTable.getColumnByName("sales_rep_id");
Column employeeId = dc.getColumnByQualifiedLabel("employees.id");
DataSet ds = dc.query()
.from(customersTable).innerJoin("employees").on(salesRepId, employeeId)
.selectCount().and("employees.name")
.groupBy("employees.name").execute();
Updates and changes
// Batch #1: Create a table and insert a few records
dc.executeUpdate(new UpdateScript() {
public void run(UpdateCallback cb) {
Table muppets = cb.createTable(schema, "muppets")
.withColumn("name").ofType(VARCHAR)
.withColumn("profession").ofType(VARCHAR).execute();
cb.insertInto(muppets).value("name","Kermit the frog")
.value("profession","TV host").execute();
cb.insertInto(muppets).value("name","Miss Piggy")
.value("profession","Diva").execute();
}
});
// Batch #2: Update and delete a record
dc.executeUpdate(new UpdateScript() {
public void run(UpdateCallback cb) {
cb.update("muppets").value("profession","Theatre host")
.where("name").equals("Kermit the frog").execute();
cb.deleteFrom("muppets")
.where("name").like("%Piggy").execute();
}
});
// Batch #3: Drop the table
dc.executeUpdate(new UpdateScript() {
public void run(UpdateCallback cb) {
cb.dropTable("muppets").execute();
}
});




