Differences between revisions 1 and 2
Revision 1 as of 2013-07-18 07:39:51
Size: 1001
Comment: Converted 'POJO datastores - Query your Java objects!' page to wiki format
Revision 2 as of 2013-07-18 08:00:36
Size: 2338
Comment:
Deletions are marked like this. Additions are marked like this.
Line 33: Line 33:
You could even have multiple collections represented as tables. For instance, if you also had this collection of Maps:

{{{#!java
Map<String,Object> record1 = new HashMap<String,Object>();
record1.put("name","Bruce Springsteen"); record1.put("title","The Boss");

Map<String,Object> record2 = new HashMap<String,Object>();
record2.put("name","Elvis Presley"); record2.put("title","The King");

List<Map<String, ?>> titles = new ArrayList<Map<String, ?>>();
titles.add(record1);
titles.add(record2);
}}}

You could make a DataContext with both the 'persons' and 'titles' collections, and join them, like this:
{{{#!java
TableDataProvider<?> provider1 = new ObjectTableDataProvider<Person>("persons", Person.class, persons);
TableDataProvider<?> provider2 = new MapTableDataProvider(
                    new SimpleTableDef("titles", new String[] {"name","title"}), titles);
DataContext dc = new PojoDataContext(Arrays.asList(provider1, provider2));

Table personsTable = dc.getTableByQualifiedLabel("persons");
Table titlesTable = dc.getTableByQualifiedLabel("titles");
DataSet dataSet = dc.query()
                    .from(personsTable).innerJoin(titlesTable)
                    .on(personsTable.getColumnByName("name"), titlesTable.getColumnByName("name"))
                    .selectAll().execute();
}}}

POJO datastores - Query your Java objects!

With the MetaModel-pojo module of MetaModel you can use MetaModel's query engine to fire queries on a collection of regular Java objects (Java beans, Maps or arrays).

Assume we have a Person class:

   1 public class Person {
   2   private String name;
   3   private int age;
   4   
   5   // getters and setters
   6 }

And some list of persons:

   1 List<Person> persons = ...

You can easily wrap this collection in a PojoDataContext and query it like a database, for instance to get all the adult names:

   1 TableDataProvider<Person> provider = new ObjectTableDataProvider<Person>("persons",Person.class,persons);
   2 DataContext dc = new PojoDataContext(provider);
   3 
   4 DataSet ds = dc.query().from("persons").select("name").where("age").greaterThan(18).execute();
   5 while (ds.next()) {
   6   Row row = ds.getRow();
   7   String name = (String) row.getValue(0);
   8   System.out.println(name);
   9 }
  10 ds.close();

You could even have multiple collections represented as tables. For instance, if you also had this collection of Maps:

   1 Map<String,Object> record1 = new HashMap<String,Object>();
   2 record1.put("name","Bruce Springsteen"); record1.put("title","The Boss");
   3 
   4 Map<String,Object> record2 = new HashMap<String,Object>();
   5 record2.put("name","Elvis Presley"); record2.put("title","The King");
   6 
   7 List<Map<String, ?>> titles = new ArrayList<Map<String, ?>>();
   8 titles.add(record1);
   9 titles.add(record2);

You could make a DataContext with both the 'persons' and 'titles' collections, and join them, like this:

   1 TableDataProvider<?> provider1 = new ObjectTableDataProvider<Person>("persons", Person.class, persons);
   2 TableDataProvider<?> provider2 = new MapTableDataProvider(
   3                     new SimpleTableDef("titles", new String[] {"name","title"}), titles);
   4 DataContext dc = new PojoDataContext(Arrays.asList(provider1, provider2));
   5 
   6 Table personsTable = dc.getTableByQualifiedLabel("persons");
   7 Table titlesTable = dc.getTableByQualifiedLabel("titles");
   8 DataSet dataSet = dc.query()
   9                     .from(personsTable).innerJoin(titlesTable)
  10                     .on(personsTable.getColumnByName("name"), titlesTable.getColumnByName("name"))
  11                     .selectAll().execute();


CategoryExamples

examples/PojoDataContext (last edited 2013-07-18 08:00:36 by KasperSorensen)