ORMLite JOINs, or rawQuery auto mapping

DanO picture DanO · Aug 16, 2012 · Viewed 12.5k times · Source

I am looking for a way to do a query that requires a JOIN. Is there any way to do this in a prepared statement, or is the rawQuery the only option that I have. If rawQuery is the only option, then is there some way to automatically map the returned objects to the objects of the Dao being implemented.

I've dug through the documents and examples but cannot find anything that will allow me to map the raw database result to an ORM object class.

Answer

Gray picture Gray · Aug 20, 2012

I am looking for a way to do a query that requires a JOIN.

ORMLite supports simple JOIN queries. You can also use raw-queries to accomplish this.

You can use the Dao.getRawRowMapper() to map the queries as you found or you can create a custom mapper. The documentation has the following sample code which shows how to map the String[] into your object:

GenericRawResults<Foo> rawResults =
  orderDao.queryRaw(
    "select account_id,sum(amount) from orders group by account_id",
    new RawRowMapper<Foo>() {
            public Foo mapRow(String[] columnNames,
              String[] resultColumns) {
                return new Foo(Long.parseLong(resultColumns[0]),
                    Integer.parseInt(resultColumns[1]));
        }
    });