1

I need to apply joins on multiple tables in Apache MetaModel. I searched through google but did not find anything related. Can some help me out on this?

niteshpandey
  • 25
  • 2
  • 6

1 Answers1

0

I guess you tried this approach:

dataContext.query().from("table1").innerJoin("table2").on("id", "table1_id")...

Indeed it doesn't offer joining more than two tables. Doing a join with the condition in the WHERE clause instead of ON should work though:

    UpdateableDataContext dataContext = new PojoDataContext();

    dataContext.executeUpdate(new UpdateScript() {

        public void run(UpdateCallback callback) {
            Table table1 = callback.createTable(callback.getDataContext().getDefaultSchema(), "table1").withColumn("id")
                    .ofType(ColumnType.INTEGER).asPrimaryKey().execute();

            Table table2 = callback.createTable(callback.getDataContext().getDefaultSchema(), "table2").withColumn("id")
                    .ofType(ColumnType.INTEGER).asPrimaryKey().withColumn("table1_id").ofType(ColumnType.INTEGER)
                    .execute();

            Table table3 = callback.createTable(callback.getDataContext().getDefaultSchema(), "table3").withColumn("id")
                    .ofType(ColumnType.INTEGER).asPrimaryKey().withColumn("table2_id").ofType(ColumnType.INTEGER)
                    .execute();

            callback.insertInto(table1).value("id", 1).execute();
            callback.insertInto(table2).value("id", 1).value("table1_id", 1).execute();
            callback.insertInto(table3).value("id", 1).value("table2_id", 1).execute();
            callback.insertInto(table3).value("id", 2).value("table2_id", 0).execute();
        }
    });

    Table table1 = dataContext.getTableByQualifiedLabel("table1");
    Table table2 = dataContext.getTableByQualifiedLabel("table2");
    Table table3 = dataContext.getTableByQualifiedLabel("table3");

    Column table1id = table1.getColumnByName("id");
    Column table2table1_id = table2.getColumnByName("table1_id");
    Column table3table2_id = table3.getColumnByName("table2_id");

    try (DataSet dataSet = dataContext.query().from(table1).and(table2).and(table3).selectAll().where(table1id).eq(table2table1_id).and(table1id).eq(table3table2_id).execute()) {
        while (dataSet.next()) {
            Row row = dataSet.getRow();
            System.out.println(row);
        }
    }
TomaszGuzialek
  • 861
  • 1
  • 8
  • 15