Don't miss our JavaOne presentation!
Java does not provide an API to directly support the expressiveness and power of SQL. All attention is given to object-relational mapping and abstractions on a higher level, such as OQL, HQL, JPQL, CriteriaQuery. jOOQ is a dual-licensed open source product that fills this gap. It implements SQL itself as a type-safe, domain-specific language in Java. jOOQ is a good choice in a Java application where SQL and the proprietary features of the database in use are important. This presentation shows how a modern domain-specific language can greatly increase developer productivity, internalizing SQL in Java.
Great Reasons for Using jOOQ
Our customers spend most time on their business-logic.
Because jOOQ takes care of all their Java/SQL infrastructure problems.
Tired 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".
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.
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.
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.
Worried about multi-schema or shared-schema multi-tenancy?
jOOQ lets you configure database schema and table overrides at runtime and also supports row-level security.
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.
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.
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.
Your data is your most important asset.
You have made a well-informed decision, when you chose a relational database management system to store your data. You are used to writing SQL to query and manipulate your data. Now, you're looking for a professional tool to help you write your SQL in Java.
With jOOQ, your database and your data come first. You want to be able to use every feature your database offers, using the language that is used to interact with databases: SQL. jOOQ will let you do precisely that.
SQL is a very unique and rich language, optimally designed to express complex queries in a simple, declarative form. When writing SQL, you will focus on what data you want to fetch, not how you want to fetch it.
jOOQ treats SQL like what it is in the first place: A language. Through its unique and modern fluent API design techniques, jOOQ embeds SQL as an internal domain-specific language directly in Java, making it easy for developers to write and read code that almost feels like actual SQL.
As an internal domain-specific language, jOOQ can leverage the powerful Java compiler and Java's generics for
- Column type checks
- Row value expression type checks
- SQL syntax checks
SQL meta data is an essential part of your code base. It is where you define table and column types, which can be used in your SQL statements in a type safe manner.
During development, SQL meta data are in constant flux. Developers add, rename, remove tables, columns, procedures, parameters all the time.
Using jOOQ's code generator, your Java code will not only compile against your actual SQL meta data, it will also immediately take notice of the changes performed to SQL meta data. This will help prevent syntax errors due to improperly changed meta data in an early phase of your development or deployment cycle.
No more productive surprises due to changed meta data!
Much of your daily work with SQL is repetitive CRUD: Creating, Reading, Updating, Deleting of database records.
jOOQ incorporates the popular Active Record paradigm by modelling each database table as a typesafe record, which is capable of storing, deleting and refreshing itself through an intuitive API.
Apart from the above operations, jOOQ's Active Records are also capable of
- Optimistic locking
- Foreign key navigation
- Mapping themselves from / to your custom POJO types
You have established a well-defined development process. You're probably using
- Development databases (maybe even one per developer)
- Test databases
- Staging databases
- Production databases
jOOQ lets you configure your environment easily, allowing you to rewrite generated SQL schema and table names.
More details can be seen in the manual:
Each SQL dialect is different in various obvious and subtle ways. These differences include things like:
- SQL statements (e.g. MERGE vs. ON DUPLICATE KEY UPDATE)
- SQL clauses (e.g. Oracle CONNECT BY vs. hierarchical CTE)
- Built-in functions (e.g. NVL, COALESCE, IFNULL, CASE .. END)
- Pseudo elements (e.g. DUAL, SYS.DUMMY, SYSIBM.DUAL, ...)
- Syntax elements (e.g. derived column lists)
Popular SQL database vendors have put a lot of effort into their most innovative and useful SQL clauses and functions and with jOOQ, you can use them very easily.
jOOQ produces an AST from your queries, which can be transformed into equivalent SQL expressions, should you chose a syntax element, which is not natively supported by your database.
The Java to SQL integration is at the core of your business. It interfaces your two most important assets:
- Your data
- Your business logic
You want to stay in full control of this interface, influencing SQL rendering, variable binding, query execution and other query operation lifecycles.
jOOQ provides you with a rich SPI to inject custom behaviour, in order to manage:
- Custom logging
- Transaction management
- Event triggers
- SQL transformation
More and more SQL databases implement some sort of procedural language for in-database data processing and bulk operations. You may even chose to move critical business logic into the database for performance, security or other reasons.
With jOOQ, stored procedures and stored functions are first-class citizens, if you chose them to be. The jOOQ code generator will generate a callable method for every routine. Stored functions can be embedded typesafely into your SQL statements.
More details can be seen in the manual:
With the jOOQ DSL, SQL looks almost as if it were natively supported by Java.
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
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
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)
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:
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: ---------------> ^^^^
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: ---------> ^^^^
A short note from Lukas
(Founder and CEO of Data Geekery)
You're probably asking yourself why we need yet another database abstraction software in Java. Fair question.
With my teams, we had written applications against large and complex Oracle databases. Hibernate was not a good fit because we wanted to stay close to SQL:2008 and to Oracle's extensions.
JDBC, on the other hand is verbose and causes a lot of quality and security headaches.
So we rolled our own tailor-made SQL builder. In fact, every company I have ever met rolled their own tailor-made SQL builder.
But our business was not to write SQL builders, our business was to write telecommunications logic. E-Banking workflows. We didn't care about infrastructure, really.
And because we didn't care, no one maintained those SQL builders. We were overwhelmed.
That's why I created jOOQ.
jOOQ is a simple way to integrate the SQL language into Java in a way that allows for developers to write safe and quality SQL fast and directly in Java such that they can again focus on their business.
Today, I'm happy to say that jOOQ is the product that I was wishing for when I started to really understand SQL. And I'm also proud to say that thousands of happy jOOQ users agree.
Why don't you give jOOQ a free try. If you don't like it you don't pay.