jOOQ is the best way to write SQL in Java
jOOQ is an internal DSL and source code generator, modelling the SQL language as a type safe Java API to help you write better SQL.
Its main features include:
Secondary features include:
MULTISET and ROW nested collections and recordsjOOQ's main feature is typesafe, embedded SQL, allowing for IDE auto completion of SQL syntax...
... as well as of schema meta data:
This allows for preventing errors of various types, including typos of identifiers:
Or data type mismatches:
The examples are from the code generation blog post.
For many more examples, please have a look at the demo. A key example showing jOOQ's various strengths is from the MULTISET operator announcement blog post:
Given these target DTOs:
record Actor(String firstName, String lastName) {}
record Film(
String title,
List<Actor> actors,
List<String> categories
) {}You can now write the following query to fetch films, their nested actors and their nested categorise in a single, type safe query:
List<Film> result =
dsl.select(
FILM.TITLE,
multiset(
select(
FILM.actor().FIRST_NAME,
FILM.actor().LAST_NAME)
.from(FILM.actor())
).as("actors").convertFrom(r -> r.map(mapping(Actor::new))),
multiset(
select(FILM.category().NAME)
.from(FILM.category())
).as("categories").convertFrom(r -> r.map(Record1::value1))
)
.from(FILM)
.orderBy(FILM.TITLE)
.fetch(mapping(Film::new));The query is completely type safe. Change a column type, name, or the target DTO, and it will stop compiling! Trust only your own eyes:
And here you see the nested result in action from the logs:
How does it work? Look at this annotated example:
…The generated SQL query might look like this, in PostgreSQL:
…This particular example is explained more in detail in the MULTISET operator announcement blog post. For many more examples, please have a look at the demo.
Wrong value returned from Record.get(TABLE.UDT.ATTRIBUTE) when there's a conflicting TABLE.ATTRIBUTE column
Add an inclusive variant of the synthetic SEEK clause (>= instead of >)
TransactionPublisher shouldn't stream intermediary results prior to committing the transaction
UpdatableRecord merge not supported with optimistic locking enabled
Add a way for a transactionPublisher to stream uncommitted intermediary output
Add support for function-based indexes in code generator
Wrong or missing runtime meta data for indexes with DESC column expressions
Implicit path join elimination doesn't happen with many-to-many relationships when the unnecessary parent table is in a one-to-one relationship with a child table
Add implicit join specific error message when path root is not declared
AbstractName::equalsIgnoreCase should not call Name::getName, which incurs a String[] allocation