Usage

In the following we'll look at how to perform actual queries against our database. Tornado Query was created with one main goal: Let you write SQL explicitly, and map the results to/from your domain objects. There was nevery any intention for Tornado Query to do magical stuff, like Hibernate, JPA and the likes.

As it turns out, describing the mappings between table columns, table relations and domain objects, you have already told Tornado Query enough to do automatic CRUD with joins, so it would be foolish to not support this. Even so, you can still do everything manually, and just use the Mappers for the actual mapping of the ResultSet.

Because of this fact, the tutorial shows you how to perform queries the manual way first, and then you'll see how Mappers can make life easier.

NOTE: It is important to realize that you can create multiple Mappers for the same domain objects. The default name of CustomerMapper.FULL indicates that the autocreated Mapper perform full joins. You can add a CustomerMapper.SIMPLE to be used in SELECTs where you don't need joins. You can also use the FULL Mapper with a manual SELECT that does not perform any joins, even if it mentions fields not in the ResultSet.

Tips and tricks

Some tricks are used across all query operations, and we'll briefly talk about some of them here.

Appending SQL to a query

When you first create a Query object, you can choose to give it some SQL right away, or you can add SQL using the add command. Both commands support varargs as well, so these are all equivalent:

Query.create("SELECT * FROM country WHERE id = 1");

Query.create("SELECT *",
         "FROM country",
         "WHERE id = 1");

Query.create()
.add("SELECT *")
.add("FROM country")
.add("WHERE id = 1");

Tornado Query will automatically introduce whitespace where needed. There are lots of mutators like addIf, addUnless, repeat etc that are further described in the SELECT part of this tutorial.