Questions tagged [jooq]

jOOQ stands for JOOQ Object Oriented Querying. jOOQ effectively combines complex SQL, typesafety, source code generation, active records, stored procedures, advanced data types, and Java in a fluent, intuitive DSL.

What's jOOQ?

jOOQ stands for JOOQ Object Oriented Querying. It combines these essential features:

  • Code Generation:

    jOOQ generates a simple Java representation of your database schema. Every table, view, stored procedure, enum, and UDT is a class.

  • Active records:

    jOOQ implements an easy-to-use active record pattern. It is not an OR-mapper, but provides a 1:1 mapping between tables/views and classes, between columns and members.

  • Typesafe SQL:

    jOOQ allows for writing compile-time typesafe querying using its built-in DSL.

  • SQL standard:

    jOOQ supports all standard SQL language features including the more complex UNIONs, nested SELECTs, joins, and aliasing.

  • Vendor-specific feature support:

    jOOQ encourages the use of vendor-specific extensions such as stored procedures, UDTs, ARRAYs, and many more.

How does jOOQ help you?

  • Your database always comes FIRST! That's where the real schema is, not in Java code or some XML mapping file with syntax you're not familiar with.
  • You keep your code DRY.
  • You won't suffer from the Object-Relational impedance mismatch.
  • You won't have syntax errors in your query.
  • You won't forget to bind variables correctly. No SQL injection, either.
  • You don't need to know your database schema by heart when you develop. The schema is generated in Java. You can use auto-completion in your IDE!
  • You don't need to create value objects for your data. Use the generated artifacts instead.
  • You have automatic type mapping between SQL data types and Java types.
  • You change something in the database? Your Java code won't compile. You don't need to wait until runtime to notice.
  • You can forget about JDBC (especially useful when dealing with UDTs, ARRAYs and stored procedures).
  • You can port your SQL to a new database. jOOQ will generate SQL that works on any database.

When to use jOOQ

jOOQ is not an OR-mapper. jOOQ is low-level relational persistence abstraction. In principle, you could even write an OR-mapper on top of jOOQ. If you're aware of this, you can benefit most from jOOQ...

  • When you love your RDBMS of choice, including all its vendor-specific features.
  • When you love control over your code.
  • When you love the relational data model (read this interesting article).
  • When you love SQL.
  • When you love stored procedures.

See the examples for yourself. You'll be convinced in no time! :-)

When not to use jOOQ

On the other hand, many people like the ease of use of Hibernate or other products, when it comes to simply persisting any domain model in any database. You should not use jOOQ...

  • When you don't care about your database (or "persistence" as it is called).
  • When you don't really need SQL.
  • When you want to map your object-oriented domain model to a database and not vice versa.
  • When you need to write DDL statements. jOOQ only supports DML statements.

What databases are supported

Every RDMBS out there has its own little specialties. We consider those specialties as much as possible, while trying to standardise the behaviour in jOOQ. In order to increase the quality of jOOQ, we run unit tests for syntax and variable binding verification, as well as integration tests for any of these databases:

  • Access
  • Aurora for MySQL
  • Aurora for PostgreSQL
  • Azure SQL Database
  • Azure SQL Data Warehouse
  • CUBRID
  • DB2
  • Derby
  • Firebird
  • H2
  • HSQLDB
  • Informix
  • Ingres
  • MariaDB
  • MySQL
  • Oracle
  • PostgreSQL
  • Redshift
  • SQLite
  • SQL Server
  • Sybase Adaptive Server Enterprise
  • Sybase SQL Anywhere
  • Teradata
  • Vertica

Similar products

When you consider jOOQ for your project, you might also have considered any of these similar products:

  • Querydsl: Focusing on the DSL and abstracting "backend integrations", such as SQL, JPA, collections, etc. The "complete LINQ of Java".
  • JaQu: Writing SQL statements using actual Java expressions

And with database tools, such as

  • ActiveJDBC: A simple mapping tool implementing ActiveRecords in a Ruby-like way

And with OR-mapping tools, such as

  • Hibernate: The mother of all inspiration to Java persistence
  • JPA: The Java EE standard

License

jOOQ is dual-licensed under the Apache Software License 2.0 and commercially licensed.

2566 questions
18
votes
1 answer

select into insert from values() with correct type casts using jOOQ

I'm using jOOQ to insert quite a few rows into a table that is a many-to-many relation. The code works, the generated SQL is as expected, my problem is that I would hope the jOOQ code could be simpler. A simplified structure I have (everything…
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
17
votes
1 answer

How do I create a Field from a value T in jOOQ, explicitly?

In jOOQ, it is easy to use bind variables in expressions like: MY_TABLE.COL.eq(1); MY_TABLE.COL.in(1, 2, 3); MY_TABLE.COL.between(1).and(10); All of the above methods accept both T and Field types, e.g. Field.eq(T) or Field.eq(Field). But…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
17
votes
2 answers

jooq single query with one to many relationship

I have a table experiment and a table tags. There may be many tags for one experiment. schema: -------- -------- |Table1| 1 n |Table2| | | <--------------> | | | | | | -------- …
Leander
  • 1,322
  • 4
  • 16
  • 31
17
votes
4 answers

JOOQ & transactions

I've been reading about transactions & jooq but I struggle to see how to implement it in practice. Let's say I provide JOOQ with a custom ConnectionProvider which happens to use a connection pool with autocommit set to false. The implementation is…
assylias
  • 321,522
  • 82
  • 660
  • 783
16
votes
1 answer

jOOQ and Spring transaction management

I use jOOQ 3.8 and Spring Boot 1.4.1. I see that jOOQ uses a mechanism to guarantee the handling of transactions. If I define a method annotated as transactional and inside a execute two insert, are they executed in the same transaction,…
mat_boy
  • 12,998
  • 22
  • 72
  • 116
16
votes
2 answers

jOOQ - support for JSR310

Does jOOQ provide support for JSR310 in combination with PostgreSQL? In particular, I am trying to use the following classes: java.time.Instant java.time.LocalDate java.time.LocalTime java.time.LocalDateTime I am storing in the following data…
Sophia Aceves
  • 187
  • 1
  • 6
16
votes
2 answers

jOOQ - difference between fetchAny and fetchOne

Is there a (real) difference between fetchAny() and fetchOne()? Both return exact one record. The API documentation is the same, but the implementation (on github) is different.
Appelsien Sap
  • 363
  • 1
  • 3
  • 9
16
votes
1 answer

Jooq Postgres JSON query

What support is there for querying into postgres json objects with JOOQ? For example, SELECT id, data->'author'->>'first_name' as author_first_name FROM books;
Joel
  • 2,601
  • 4
  • 33
  • 44
15
votes
1 answer

Select constant in JOOQ union

I need to do the equivalent of this in JOOQ. SELECT name, 'companyType' AS resultType FROM company UNION ALL SELECT name, 'userType' AS resultType FROM "user"; I have figured out how to do unionall in JOOQ fine but I cannot figure out how…
benstpierre
  • 32,833
  • 51
  • 177
  • 288
14
votes
2 answers

How to write LEFT OUTER JOIN on the same table in jOOQ?

how to write following SQL using jOOQ? SELECT * FROM food_db_schema.tblCategory AS t1 LEFT OUTER JOIN food_db_schema.tblCategory AS t2 ON t1.category_id = t2.parent_id WHERE t2.parent_id IS NULL AND t1.heartbeat = "ALIVE"; database is mySQL
Charis997
  • 335
  • 1
  • 6
  • 11
14
votes
2 answers

How to write OPTIONAL where clauses in JOOQ

List result = DSL.using(configuration()) .select() .from(MY_TABLE) .where(MY_TABLE.ID1.equal(pk_id1)) .and(MY_TABLE.ID2.equal(fk_id2)) …
Luke Xu
  • 2,302
  • 3
  • 19
  • 43
14
votes
2 answers

JOOQ empty condition

Is there way to define empty condition which produces nothing in output sql query? Now, for default condition I use trueCondition() private static Condition limitCondition(Integer offset, Integer count) { Condition limitCondition =…
user3278460
14
votes
2 answers

H2 - How to create a database trigger that log a row change to another table?

How to create a database trigger that log a row change to another table in H2? In MySQL, this can be done easily: CREATE TRIGGER `trigger` BEFORE UPDATE ON `table` FOR EACH ROW BEGIN INSERT INTO `log` ( `field1` `field2`, …
Pinch
  • 2,768
  • 3
  • 28
  • 44
14
votes
1 answer

JDBC getUpdateCount is returning 0, but 1 row is updated, in SQL Server

Anyone ever seen this? I'm using MS SQL Server 2008, and I've tried it with two different JDBC drivers (jtds, and Microsoft's). I run a simple statement to update a row, and it does update it, but getUpdateCount returns 0. If I try it for different…
Rob N
  • 15,024
  • 17
  • 92
  • 165
13
votes
1 answer

DISTINCT ON() in jOOQ

I would like to make a query in PostgreSQL select distinct on(uuid) (select nr_zew from bo_get_sip_cti_polaczenie_info(uuid)) as nr_zew from bo_sip_cti_event_day where data_ins::date = current_date and kierunek like 'P' and (hangup_cause like…
Michu93
  • 5,058
  • 7
  • 47
  • 80