Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsMay 23, 2026·3 min de lecture

jOOQ — Type-Safe SQL in Java with Code Generation

jOOQ generates Java code from your database schema and provides a fluent DSL for writing type-safe SQL queries, combining the power of raw SQL with compile-time validation.

Prêt pour agents

Installation avec revue préalable

Cet actif nécessite une revue. Le prompt copié demande un dry-run, affiche les écritures, puis continue seulement après confirmation.

Needs Confirmation · 64/100Policy : confirmer
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
jOOQ Overview
Commande avec revue préalable
npx -y tokrepo@latest install f8275aae-5660-11f1-9bc6-00163e2b0d79 --target codex

Dry-run d'abord, confirmez les écritures, puis lancez cette commande.

Introduction

jOOQ (Java Object Oriented Querying) takes the approach that SQL is a first-class citizen. It reverse-engineers your database schema into generated Java classes, then provides a fluent API that mirrors SQL syntax. Queries are validated at compile time against your actual schema, catching column name typos and type mismatches before runtime.

What jOOQ Does

  • Generates Java classes from existing database schemas via JDBC introspection
  • Provides a fluent DSL that mirrors SQL syntax for SELECT, INSERT, UPDATE, DELETE, and DDL
  • Validates queries at compile time using generated schema metadata
  • Supports advanced SQL features like window functions, CTEs, recursive queries, and MERGE
  • Works with PostgreSQL, MySQL, MariaDB, Oracle, SQL Server, SQLite, H2, and more

Architecture Overview

jOOQ's code generator connects to your database, reads the schema catalog, and produces Java classes representing tables, columns, sequences, and routines. The DSL layer wraps these generated types in a query builder that constructs a SQL abstract syntax tree. At execution time, the AST is rendered to the target dialect's SQL string, bound with parameters, and sent via JDBC. This two-phase approach keeps queries database-portable while leveraging vendor-specific features.

Installation & Configuration

  • Add jooq, jooq-meta, and jooq-codegen dependencies via Maven or Gradle
  • Configure the code generator with JDBC URL and target package in pom.xml or build.gradle
  • Run code generation as a build step to keep generated classes in sync with the schema
  • Use DSL.using(dataSource, SQLDialect.POSTGRES) to create a DSLContext
  • Integrate with Spring Boot via spring-boot-starter-jooq for auto-configuration

Key Features

  • SQL-centric: write SQL, not string concatenation, with full IDE autocomplete
  • Database-first code generation ensures code matches the actual schema
  • Dialect-aware rendering translates queries to the target database's SQL variant
  • Active Record pattern for simple CRUD alongside the DSL for complex queries
  • Comprehensive support for stored procedures, UDTs, and database-specific types

Comparison with Similar Tools

  • Hibernate/JPA — object-first ORM that abstracts SQL away; jOOQ embraces SQL and generates code from the schema
  • MyBatis — XML-mapped SQL with string templates; jOOQ provides compile-time type safety
  • Exposed — Kotlin-native DSL without code generation; jOOQ generates classes from the database
  • JDBC Template — manual SQL with string parameters; jOOQ adds type safety and a fluent API
  • QueryDSL — similar type-safe approach but less actively maintained; jOOQ has broader dialect support

FAQ

Q: Is jOOQ open source? A: The open-source edition supports open-source databases (PostgreSQL, MySQL, SQLite, H2). Commercial databases require a paid license.

Q: How often should I regenerate code? A: Run code generation after every schema migration to keep Java classes in sync with the database.

Q: Can I use jOOQ without code generation? A: Yes. The plain SQL API and DSL work without generated code, but you lose compile-time schema validation.

Q: Does jOOQ work with Kotlin? A: Yes. jOOQ's fluent API works naturally in Kotlin, and some users pair it with coroutines for async database access.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires