All versions: 3.11 | 3.10 | 3.9 | 3.8 | 3.7 | Development versions: 3.12 | Unsupported versions: 3.6 | 3.5 | 3.4 | 3.3 | 3.2 | 2.6

Navigation methods

When using jOOQ's code generator along with its record generation capabilities, generated records can have navigation methods contained in them, if properly configured. These navigation methods allow for "navigating" inbound or outbound foreign key references by executing an appropriate query. An example is given here:

CREATE TABLE book (
  AUTHOR_ID NUMBER(7) NOT NULL,
  
  -- [...]
  
  FOREIGN KEY (AUTHOR_ID) REFERENCES author(ID)
)
BookRecord book = create.fetch(BOOK, BOOK.ID.eq(5));

// Find the author of a book
AuthorRecord author = book.fetchAuthor();

// Find other books by that author
List<BookRecord> books = author.fetchBookList();

These methods are safe for use with several foreign keys referencing the same tables:

CREATE TABLE book (
  AUTHOR_ID NUMBER(7) NOT NULL,
  CO_AUTHOR_ID NUMBER(7) NOT NULL,
  
  -- [...]
  
  FOREIGN KEY (AUTHOR_ID) REFERENCES author(ID),
  FOREIGN KEY (CO_AUTHOR_ID) REFERENCES author(ID)
)
BookRecord book = create.fetch(BOOK, BOOK.ID.eq(5));

// Find the author of a book
AuthorRecord author   = book.fetchAuthorByAuthorId();
AuthorRecord coAuthor = book.fetchAuthorByCoAuthorId();

// Find other books by those authors
List<BookRecord> books = author.fetchBookListByAuthorId();
List<BookRecord> books = coAuthor.fetchBookListByCoAuthorId();

Note that, unlike in Hibernate, jOOQ's generated navigation methods will always lazy-fetch relevant records, without caching any results. In other words, every time you run such a fetch method, a new query will be issued.

These fetch methods only work on "attached" records. See the manual's section about serializability for some more insight on "attached" objects.

The jOOQ Logo