You are on page 1of 4

learn

download

support

community

news

blog

about us

Get Back in Control of Your SQL


jOOQ generates Java code from your database and lets you build typesafe SQL queries through its fluent API.

Great Reasons for Using jOOQ


Database First
T ired of ORMs driving your database model? Whether you design a new application or integrate with your legacy, your database holds your most important asset: your data. jOOQ is SQL-centric. Your database comes "first".

Typesafe SQL
Fed up with detecting SQL syntax errors in production? SQL is a highly expressive and type safe language with a rich syntax. jOOQ models SQL as an internal DSL and uses the Java compiler to compile your SQL syntax, metadata and data types.

Code Generation
Bored with renaming table and column names in your Java code? jOOQ generates Java classes from your database metadata. Your Java compiler will tell you when your code is out of sync with your schema.

Active Records
Annoyed by the amount of SQL you write for CRUD? jOOQ lets you perform CRUD and POJO mapping directly on Active Records, which are also generated from the code generator.

Multi-Tenancy
Worried about multi-schema or shared-schema multitenancy? jOOQ lets you configure database schema and table overrides at runtime and also supports row-level security.

Standardisation
Overwhelmed by the subtle differences in SQL dialects? jOOQ performs SQL transformation to transform common SQL expressions into your database's closest match. Write SQL that works on all your databases.

Query Lifecycle
Irritated by your ORM's mysterious SQL generation? jOOQ lets you hook into its SQL generation lifecycle, for logging, transaction handling, ID generation, SQL transformation and much more.

Procedures
Surprised by your ORM's lack of support for stored procedures? Stored Procedures are an essential feature of modern SQL databases. jOOQ lets you embed stored function calls into your SQL statements.

Java Database Framework Comparison

jOOQ SQL related


SQL centric SQL dialect abstraction SQL dialect emulation

Hibernate

MyBatis

SpringData

JDBC

[1]

[2]

Database related
Stored procedures User-defined types [3] [3] [3] [4] [3] [4]

Java API related


Native SQL support T ypesafe query DSL Metadata code generation Active records Query lifecycle SPI Schema, table multitenancy SQL transformation SPI SQL templating POJO mapping Row mapping [6] [7] [7] [5]

Remarks
1. Hibernate evolves around HQL and/or JPQL. While it does not hide SQL entirely, it does not really help writing SQL. 2. Spring Data is not focused on SQL, which means that you load lots of non-related dependencies and complexity from other types of data stores. 3. You can make stored procedures work with all frameworks. But it is a pain, compared to jOOQ. 4. You can make user-defined types work with JDBC. But it is a pain, compared to jOOQ. 5. Hibernate inherits JPA's Criteria API, which is a typesafe query DSL, although not for SQL. 6. jOOQ has a simple templating engine that can be used with plain SQL. 7. jOOQ's and MyBatis' have simple POJO mapping capabilities.

Examples
With the jOOQ DSL, SQL looks almost as if it were natively supported by Java. For instance, get all books published in 2011, ordered by title

SELECT * FROM BOOK WHERE BOOK.PUBLISHED_IN = 2011 ORDER BY BOOK.TITLE

create.selectFrom(BOOK) .where(BOOK.PUBLISHED_IN.eq(2011)) .orderBy(BOOK.TITLE)

jOOQ also supports more complex SQL statements. get all authors' first and last names, and the number of books they've written in German, if they have written more than five books in German in the last three years (from 2011), and sort those authors by last names limiting results to the second and third row, then lock first and last names columns for update

SELECT AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, COUNT(*) FROM AUTHOR JOIN BOOK ON AUTHOR.ID = BOOK.AUTHOR_ID WHERE BOOK.LANGUAGE = 'DE' AND BOOK.PUBLISHED > DATE '2008-01-01' GROUP BY AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME HAVING COUNT(*) > 5 ORDER BY AUTHOR.LAST_NAME ASC NULLS FIRST LIMIT 2 OFFSET 1 FOR UPDATE OF AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME

create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count()) .from(AUTHOR) .join(BOOK).on(AUTHOR.ID.equal(BOOK.AUTHOR_ID)) .where(BOOK.LANGUAGE.eq("DE")) .and(BOOK.PUBLISHED.gt(date("2008-01-01"))) .groupBy(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) .having(count().gt(5)) .orderBy(AUTHOR.LAST_NAME.asc().nullsFirst()) .limit(2) .offset(1) .forUpdate() .of(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)

Typesafety Examples
SQL is a very type safe language. So is jOOQ. jOOQ uniquely respects SQL's row value expression typesafety. jOOQ will use your Java compiler to type-check the following:

Predicates
jOOQ type-checks simple comparison predicates and predicates with subqueries.

select().from(t).where(t.a.eq(select(t2.x).from(t2)); // Type-check here: ---------------> ^^^^ select().from(t).where(t.a.eq(any(select(t2.x).from(t2))); // Type-check here: -------------------> ^^^^ select().from(t).where(t.a.in(select(t2.x).from(t2)); // Type-check here: ---------------> ^^^^

Set Operations
jOOQ type-checks degree and data types of union subselects.

select(t1.a).from(t1).unionAll(select(t2.a).from(t2)); // Type-check here: ----------------> ^^^^ select(t1.a, t1.b).from(t1).union(select(t2.a, t2.b).from(t2)); // Type-check here: -------------------> ^^^^^^^^^^

Some more sophisticated examples show type-checks on row value expressions:

SELECT * FROM t WHERE (t.a, t.b) = (1, 2) SELECT * FROM t WHERE (t.a, t.b) OVERLAPS (date1, date2) SELECT * FROM t WHERE (t.a, t.b) IN (SELECT x, y) UPDATE t SET (a, b) = (SELECT x, y FROM t2 WHERE ...) INSERT INTO t (a, b) VALUES (1, 2)

select().from(t).where(row(t.a, t.b).eq(1, 2)); // Type-check here: -----------------> ^^^^ select().from(t).where(row(t.a, t.b).overlaps(date1, date2)); // Type-check here: ------------------------> ^^^^^^^^^^^^ select().from(t).where(row(t.a, t.b).in(select(t2.x, t2.y))); // Type-check here: -------------------------> ^^^^^^^^^^ update(t).set(row(t.a, t.b), select(t2.x, t2.y).where(...)); // Type-check here: --------------> ^^^^^^^^^^ insertInto(t, t.a, t.b).values(1, 2); // Type-check here: ---------> ^^^^

DOWNLOAD
Get a copy of jOOQ 3.3 now!

Documentation
Tutorial The manual (s ing le pag e) The manual (multi pag e) The manual (PDF) Javadoc Us ing SQL in Java is s imple! Convince your manag er!

Community
Us er Groups Blog GitHub Stack Overflow

Support
Community Support Commercial Support Training s Cus tom Eng ineering Contact News letter

Legal
Site Notice Licens es Privacy Policy Terms of Service

2009 - 2014 by Data Geekery GmbH. All rig hts res erved. jOOQ is a trademark of Data Geekery GmbH. All other trademarks and copyrig hts are the property of their res pective owners .

You might also like