Building new table objects (including joint tables) - Zalizyaka-Ney/handy.storage GitHub Wiki
What if you want to read from a database some data different from models that are declared as tables? For example, you want to get data about number of books published by each author. To do that, declare a new model:
public class AuthorPublications implements Model {
@Column
private Author author;
@Column
@FunctionResult(type = Function.COUNT)
private int numberOfBooks;
}
than create a new table object and perform a query:
WritableTable<Book> booksTable = database.getTable(Book.class);
ReadableTable<AuthorPublications> authorPublicationsTable = booksTable.asReadableTable(AuthorPublications.class);
List<AuthorPublications> authorPublications = authorPublicationsTable.select().groupBy("author").execute();
An analogous approach is used for reading from a join of tables:
ReadableTable<JoinedModel> joinedTable = tableLeft.join(tableRight).using(SOME_COLUMN).asReadableTable(JoinedModel.class);
List<JoinedModel> data = joinedTable.selectAll();