Over a million developers have joined DZone.

JOOQ 4.0’s New API Will Use Annotations Only for Truly Declarative Java/SQL Programming

DZone's Guide to

JOOQ 4.0’s New API Will Use Annotations Only for Truly Declarative Java/SQL Programming

The title says it all... JOOQ 4.0’s new API will use Annotations only for truly declarative Java/SQL programming. Read on for more details.

· Database Zone ·
Free Resource

Download the Altoros NoSQL Performance Benchmark 2018. Compare top NoSQL solutions – Couchbase Server v5.5, MongoDB v3.6, and DataStax Enterprise v6 (Cassandra).

Before you read on, please do note that this article was originally posted on April 1st here, before being syndicated to DZone's MVB programme. I.e. take it with a grain of salt:



SQL is the only really popular and mature 4GL (Fourth Generation Programming Language). Therefore, it is the only popular declarative language.

At the same time, SQL has proven that Turing completeness is not reserved to lesser languages like C, C++, or Java. Since SQL:1999 and its hierarchical common table expressions, SQL can be safely considered "Turing complete". This means that any program can be written in SQL. Don’t believe it? Take, for instance, this SQL Mandelbrot set calculation as can be seen in this Stack Overflow question.

mandelbrot set

Source: User Elie on http://stackoverflow.com/q/314864/521799

Wonderful! No more need for procedural, and object oriented cruft.

How We’ve Been Wrong So Far…

At Data Geekery (the company behind jOOQ), we love SQL. And, we love Java. But, one thing has always bothered us in the past. Java is not really a purely declarative language. A lot of Java language constructs are real anti-patterns for the enlightened declarative programmer. For instance:

// This is bad
for (String string : strings)

// This is even worse
try {
catch (SQLException e) {

The imperative style of the above code is hardly ever useful. Programmers need to tediously tell the Java compiler and the JVM what algorithm they meant to implement down to the single statement, when, in reality, using the JIT and other advanced optimization techniques, they don’t really have to.

Luckily, There Are Annotations

Since Java 5, however, there have been farsighted people in expert groups who have added a powerful new concept to the Java language: Annotations (more info here). At first, experiments were made with only a handful of limited-use annotations, like:

  • @Override
  • @SuppressWarnings

But then, even more farsighted people have then proceeded in combining these annotations to form completely declaratively things like a component:

@WebServlet(urlPatterns = "/MonsterServlet")
@Table(name = "MonsterEntity")
@NamedQuery(name = "findAll", query = "SELECT c FROM Book c")
public class Book extends HttpServlet {

 // ======================================
 // = Attributes =
 // ======================================

 private Long id;
 private String isbn;
 private Integer nbOfPage;
 private Boolean illustrations;
 private String contentLanguage;
 @Column(nullable = false)
 @Size(min = 5, max = 50)
 @XmlElement(nillable = false)
 private String title;
 private Float price;
 @Column(length = 2000)
 @Size(max = 2000)
 private String description;
 @CollectionTable(name = "tags")
 private List<String> tags = new ArrayList<>();

Look at this beauty. Credits to Antonio Goncalves

However, we still think that there is a lot of unnecessary object oriented bloat in the above. Luckily, recent innovations that make Java annotations Turing complete (or even sentient?) will now finally allow us to improve upon this situation, specifically for jOOQ, which aims to model the declarative SQL language in Java. Finally, annotations are a perfect fit!

Those innovations are:

These innovations allow us to completely re-implement the entire jOOQ 4.0 API in order to allow for users writing SQL as follows:

 table = @Table("AUTHOR"),
 join = @Join("BOOK"),
 predicate = @On(
 left = @Column("AUTHOR.ID"),
 op = @Eq,
 right = @Column("BOOK.AUTHOR_ID")
 predicate = @Predicate(
 left = @Column("BOOK.TITLE"),
 op = @Like,
 right = @Value("%Annotations in a Nutshell%")
class SQLStatement {}

Just like JPA, this makes jOOQ now fully transparent and declarative by using annotations. Developers will now be able to completely, effortlessly translate their medium to highly complex SQL queries into the exact equivalent in jOOQ annotations.

Don’t worry, we’ll provide migration scripts to upgrade your legacy jOOQ 3.x application to 4.0. A working prototype is on the way and is expected to be released soon—early adopter feedback is very welcome, so stay tuned for more exciting SQL goodness!

Download the whitepaper, Moving From Relational to NoSQL: How to Get Started. We’ll take you step by step through your first NoSQL project.

jooq ,sql ,turing test ,api ,java ,declarative programming

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}