1. Introduction

jOOQ (Java Object Oriented Querying) is a powerful library that simplifies database interaction in Java by enabling us to write SQL queries in an object-oriented manner. Joining tables is a fundamental operation in relational databases, allowing us to combine data from multiple tables based on a specific condition. In this tutorial, we’ll explore various types of joins available in jOOQ.

2. Setting up jOOQ

Joining two tables using jOOQ involves utilizing the DSL (Domain Specific Language) provided by jOOQ to construct SQL queries.

To use jOOQ, we’ll need to add the jOOQ and PostgreSQL dependencies to our Maven project’s pom.xml file:

<dependency>
    <groupId>org.jooq</groupId>
    <artifactId>jooq</artifactId>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

Before using joins, we need to establish a connection to the database using jOOQ. Let’s create a method getConnection() to obtain the DSLContext object for database interaction:

public static DSLContext getConnection() {
    try {
        Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        DSLContext context = DSL.using(conn, SQLDialect.POSTGRES);
        return context;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

We’ll use the context object throughout the tutorial to interact with the database:

DSLContext context = DBConnection.getConnection();

Additionally, jOOQ provides a code generator that generates Java classes based on our database schema. We’ll assume that the tables Store, Book and BookAuthor are created in the database with their respective schemas.

Next, we can insert test data using the DSLContext object within a method annotated @BeforeClass to ensure it runs before each test. Let’s integrate the test data insertion into our setup method:

@BeforeClass
public static void setUp() throws Exception {
    context = DBConnection.getConnection();
    
    context.insertInto(Tables.STORE, Store.STORE.ID, Store.STORE.NAME)
      .values(1, "ABC Branch I ")
      .values(2, "ABC Branch II")
      .execute();

    context.insertInto(Tables.BOOK, Book.BOOK.ID, Book.BOOK.TITLE, Book.BOOK.DESCRIPTION, 
      Book.BOOK.AUTHOR_ID, Book.BOOK.STORE_ID)
      .values(1, "Article 1", "This is article 1", 1, 1)
      .values(2, "Article 2", "This is article 2", 2, 2)
      .values(3, "Article 3", "This is article 3", 1, 2)
      .values(4, "Article 4", "This is article 4", 5, 1)
      .execute();

    context.insertInto(Tables.BOOKAUTHOR, Bookauthor.BOOKAUTHOR.ID, Bookauthor.BOOKAUTHOR.NAME, 
      Bookauthor.BOOKAUTHOR.COUNTRY)
      .values(1, "John Smith", "Japan")
      .values(2, "William Walce", "Japan")
      .values(3, "Marry Sity", "South Korea")
      .values(4, "Morry Toh", "England")
      .execute();
}

3. Using the join Clause

In jOOQ, SelectJoinStep<Record> is an interface that represents a step within the process of building a SELECT query with joins. We can use methods like select() to specify which columns we want to retrieve from the tables involved.

The join() method in jOOQ is used to perform an inner join between tables based on a specified condition. An inner join retrieves rows where a specific condition is met in both tables.

Here’s an example of joining the Book and BookAuthor tables based on the author ID:

SelectJoinStep<Record> query = context.select()
  .from(Tables.BOOK)
  .join(Tables.BOOKAUTHOR)
  .on(field(Tables.BOOK.AUTHOR_ID).eq(field(Tables.BOOKAUTHOR.ID)));

assertEquals(3, query.fetch().size());

Here’s an extended example to demonstrate joining multiple tables:

SelectJoinStep<Record> query = context.select()
  .from(Tables.BOOK)
  .join(Tables.BOOKAUTHOR)
  .on(field(Tables.BOOK.AUTHOR_ID).eq(field(Tables.BOOKAUTHOR.ID)))
  .join(Tables.STORE)
  .on(field(Tables.BOOK.STORE_ID).eq(field(Tables.STORE.ID)));

assertEquals(3, query.fetch().size());

We added another join to the Store table. This join operation connects the Book and Store tables based on the STORE_ID column in the Book table and the ID column in the Store table. By adding this additional join, the query now retrieves data from three tables: Book, BookAuthor, and Store.

4. Using Outer Joins

jOOQ supports various join types beyond the default inner join, such as outer joins. Outer joins allow us to retrieve records even if there is no matching record in the joined table.

4.1. Left Outer Join

A left join includes all rows from the left table Book and matching rows from the right table BookAuthor. Any unmatched rows from the right table will have null values for columns specific to authors.

Let’s see how to perform a left outer join using jOOQ:

SelectJoinStep<Record> query = context.select()
  .from(Tables.BOOK)
  .leftOuterJoin(Tables.BOOKAUTHOR)
  .on(field(Tables.BOOK.AUTHOR_ID).eq(field(Tables.BOOKAUTHOR.ID)));

assertEquals(4, query.fetch().size());

In the output, the last row’s author column displays null instead of a corresponding author entry:

+----+---------+---------+-----------------+--------+------+-------------+-------+
|  id|author_id|title    |description      |store_id|    id|name         |country|
+----+---------+---------+-----------------+--------+------+-------------+-------+
|   1|        1|   Book 1|This is    book 1|       1|     1|John Smith   |Japan  |
|   2|        2|   Book 2|This is    book 2|       2|     2|William Walce|Japan  |
|   3|        1|   Book 3|This is    book 3|       2|     1|John Smith   |Japan  |
|   4|        5|   Book 4|This is    book 4|       1|{null}|{null}       |{null} |
+----+---------+---------+-----------------+--------+------+-------------+-------+

When performing a left outer join, as demonstrated in the query, all rows from the left table Book are included in the result set. In this case, even though there is no matching author_id in the BookAuthor table for the last row, it still appears in the output. However, since there are no corresponding data available in the BookAuthor table, the columns specific to authors (id, name, country) have null values for this row.

4.2. Right Outer Join

In contrast, a right join encompasses all rows from the right table BookAuthor and matches them with rows from the left table Book. Rows from the left table that don’t match any entries in the right table will have null values for book-specific columns.

Let’s see how to perform a right outer join using jOOQ:

SelectJoinStep<Record> query = context.select()
  .from(Tables.BOOK)
  .rightOuterJoin(Tables.BOOKAUTHOR)
  .on(field(Tables.BOOK.AUTHOR_ID).eq(field(Tables.BOOKAUTHOR.ID)));

assertEquals(5, query.fetch().size());

Similarly to the left outer join, in the output, the last two authors don’t have associated book records, resulting in null values:

+------+---------+---------+-----------------+--------+----+-------------+-----------+
|    id|author_id|title    |description      |store_id|  id|name         |    country|
+------+---------+---------+-----------------+--------+----+-------------+-----------+
...
|{null}|   {null}|{null}   |{null}           |  {null}|   4|Morry Toh    |England    |
|{null}|   {null}|{null}   |{null}           |  {null}|   3|Marry Sity   |South Korea|
+------+---------+---------+-----------------+--------+----+-------------+-----------+

4.3. Full Outer Join

A full outer join combines all rows from both tables Book and BookAuthor, regardless of whether there’s a match. Rows that don’t have a match in the opposite table have null values for columns from that table.

To perform a full outer join in jOOQ, we can use the following syntax:

SelectJoinStep<Record> query = context.select()
  .from(Tables.BOOK)
  .fullOuterJoin(Tables.BOOKAUTHOR)
  .on(field(Tables.BOOK.AUTHOR_ID).eq(field(Tables.BOOKAUTHOR.ID)));

assertEquals(6, query.fetch().size());

5. Using Natural Joins

Natural joins automatically determine the join condition based on matching column names. This can be helpful when the join condition is straightforward using a common column like AUTHOR_ID:
SelectJoinStep<Record> query = context.select()
  .from(Tables.BOOK)
  .naturalJoin(Tables.BOOKAUTHOR);

assertEquals(4, query.fetch().size());
However, if column names aren’t intended for joining or data types don’t match, unintended results might occur. In the output, we observe that one of the records was matched incorrectly:
+----+---------+---------+-----------------+--------+----+-------------+-------+
|  id|author_id|title    |description      |store_id|  id|name         |country|
+----+---------+---------+-----------------+--------+----+-------------+-------+
...
|   4|        5|   Book 4|This is    book 4|       1|   4|Morry Toh    |England|
+----+---------+---------+-----------------+--------+----+-------------+-------+

6. Using Cross Joins

Cross joins are the most basic type of join, where every row from one table is combined with every row from the other table. This can be useful in specific scenarios where we have a table of Store and Book. We want to display a list of all possible store-book combinations.

Let’s examine the outcome when we execute a cross join:

SelectJoinStep<Record> query = context.select()
  .from(Tables.STORE)
  .crossJoin(Tables.BOOK);

assertEquals(8, query.fetch().size());

A cross join efficiently produces every possible combination, enabling us to showcase options like “Branch I – Book 1“, “Branch I – Book 2“, and so forth. However, cross joins should be used cautiously due to the potential for creating very large datasets, especially if the tables involved have many rows.

7. Conclusion

In this article, we learned how to join tables in jOOQ. We discussed various types of joins, including inner joins, outer joins (left, right, and full outer), natural joins, and cross joins. Moreover, we saw that natural joins and cross joins can be useful but should be used carefully due to potential unintended results or performance issues, especially with large datasets.

As always, the source code for the examples is available over on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
res – Persistence (eBook) (cat=Persistence)
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.