No More MultipleBagFetchException Thanks to Multiset Nested Collections

I've recently stumbled upon this interesting Stack Overflow question about Hibernate's popular MultipleBagFetchException. The question is super popular, and the answers are plenty. The various limitations are discussed throughout the question, it all boils down to a simple fact: Joins are the wrong tool to nest collections. Given a schema like the Sakila database: There … Continue reading No More MultipleBagFetchException Thanks to Multiset Nested Collections

Approximating e With SQL

If you're running on PostgreSQL, you could try the following cool query: WITH RECURSIVE r (r, i) AS ( SELECT random(), i FROM generate_series(1, 1000000) AS t (i) ), s (ri, s, i) AS ( SELECT i, r, i FROM r UNION ALL SELECT s.ri, r.r + s.s, s.i + 1 FROM r JOIN s … Continue reading Approximating e With SQL

Traversing jOOQ Expression Trees with the new Traverser API

Starting from jOOQ 3.16, we're investing a lot into opening up our internal query object model (QOM) as a public API. This is mainly useful for people who use jOOQ's parser and wish to access the parsed expression tree, or to transform SQL, e.g. to implement row level security in jOOQ. But occasionally, even with … Continue reading Traversing jOOQ Expression Trees with the new Traverser API

Detect Accidental Blocking Calls when Using R2DBC

A while ago, jOOQ has added the org.jetbrains:annotations dependency to the jOOQ API, in order to annotate return types with nullability information. For example, the entire DSL is non-nullable: public interface SelectWhereStep<R extends Record> extends SelectConnectByStep<R> { @NotNull @CheckReturnValue @Support SelectConditionStep<R> where(Condition condition); // ... } It makes sense to give this guarantee especially to … Continue reading Detect Accidental Blocking Calls when Using R2DBC

A Rarely Seen, but Useful SQL Feature: CORRESPONDING

I recently stumbled upon a standard SQL feature that was implemented, to my surprise, in HSQLDB. The keyword is CORRESPONDING, and it can be used with all set operations, including UNION, INTERSECT, and EXCEPT. Let's look at the sakila database. It has 3 tables with people in it: CREATE TABLE actor ( actor_id integer NOT … Continue reading A Rarely Seen, but Useful SQL Feature: CORRESPONDING

Using jOOQ’s DiagnosticsConnection to detect N+1 Queries

N+1 queries are a popular problem in many applications that run SQL queries. The problem can be described easily as follows: 1 query fetching a parent value is runN queries fetching each individual child values are run This problem isn't limited to SQL, it can happen with any poorly designed API that does not allow … Continue reading Using jOOQ’s DiagnosticsConnection to detect N+1 Queries

The Useful BigQuery * EXCEPT Syntax

One of the coolest things about using and making jOOQ is that we get to discover the best extensions to the standard SQL language by vendors, and add support for those clauses in jOOQ via emulations. One of these syntaxes is BigQuery's * EXCEPT syntax. Everyone who ever wrote ad-hoc SQL queries would have liked … Continue reading The Useful BigQuery * EXCEPT Syntax

3.16.0 Release with a new Public Query Object Model API, Spatial Support, YugabyteDB Support and Much More

This release tackles two long standing and complex feature requests that usershave asked us to offer for a long time: a public API for manipulating jOOQ'squery object model (QOM), and spatial support. New Query Object Model (QOM) Every jOOQ query is modeled as an expression tree constructed via our intuitiveDSL. For some use-cases there exist … Continue reading 3.16.0 Release with a new Public Query Object Model API, Spatial Support, YugabyteDB Support and Much More

How to customise a jOOQ Configuration that is injected using Spring Boot

Starting from Spring Boot 2.5, there's a handy new callback that you can implement, called DefaultConfigurationCustomizer, where the word DefaultConfiguration corresponds to jOOQ's DefaultConfiguration. You can simply create a class like this in your project: import org.jooq.conf.RenderQuotedNames; import org.jooq.impl.DefaultConfiguration; import org.springframework.boot.autoconfigure.jooq.*; import org.springframework.context.annotation.*; @Configuration public class Config { @Bean public DefaultConfigurationCustomizer configurationCustomiser() { return (DefaultConfiguration … Continue reading How to customise a jOOQ Configuration that is injected using Spring Boot

Using JDK Collectors to De-duplicate parent/child nested collections

In classic SQL (i.e. before jOOQ's awesome MULTISET operator), nested collections were fetched using ordinary (outer) joins. An example of such a query would be a query running against the sakila database to fetch actors and their films. Using jOOQ: Result<?> result = ctx.select( ACTOR.ACTOR_ID, ACTOR.FIRST_NAME, ACTOR.LAST_NAME, FILM.FILM_ID, FILM.TITLE) .from(ACTOR) .leftJoin(FILM_ACTOR).on(ACTOR.ACTOR_ID.eq(FILM_ACTOR.ACTOR_ID)) .leftJoin(FILM).on(FILM_ACTOR.FILM_ID.eq(FILM.FILM_ID)) .orderBy( ACTOR.ACTOR_ID, FILM.FILM_ID) … Continue reading Using JDK Collectors to De-duplicate parent/child nested collections