Skip to content

One-to-many select in Jooq

I am trying out JOOQ and trying to select from 3 tables (Author, Books and Articles) using a join statement. The ERD is as follows:

Author ----< Books

The query I have is the following:

    final List<Tuple3<AuthorRecord, BooksRecord, ArticlesRecord>> tupleList =
            .map(r -> Tuple.tuple(r.into(Author.AUTHOR).into(AuthorRecord.class),

I also have a protobuf object as follows:

message Author {
    int64 id = 1;
    string name = 2;
    repeated string books = 3;
    repeated string articles = 4;

(or any other pojo for that matter) which will hold all the entities (author details + list of books + list of articles) into one object. My question is, is there some way to map out of the box all three tables into one object using JOOQ.

Thanks in advance.



Using JOIN doesn’t work for this.

Your query will be rather inefficient because if you’re using joins this way, you’re creating a cartesian product between the books and the articles table, resulting in quite some memory and CPU consumption both in the database and in your Java client, before you de-duplicate all the meaningless combinations.

The “correct” SQL approach would be to use MULTISET as described in this article here. Unfortunately, jOOQ 3.9 doesn’t support MULTISET yet (nor do many databases). So, you should create two separate queries:

  1. Fetching all the books
  2. Fetching all the articles

And then use something like Java 8 Streams to map them into a single object.

Using SQL/XML or SQL/JSON starting from jOOQ 3.14

Luckily, starting from jOOQ 3.14, there’s an out-of-the-box solution to this style of nesting collections via SQL/XML or SQL/JSON, if your RDBMS supports that. You can produce a document, and then use something like Gson, Jackson, or JAXB to map it back to your Java classes. For example:

List<Author> authors =
2 People found this is helpful