New versions: Dev (3.16) | Latest (3.15) | 3.14 | 3.13 | 3.12 | 3.11 | 3.10 | 3.9 | 3.8 | Old versions: 3.7 | 3.6

JPADatabase: Code generation from entities

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

Many jOOQ users use jOOQ as a complementary SQL API in applications that mostly use JPA for their database interactions, e.g. to perform reporting, batch processing, analytics, etc.

In such a setup, you might have a pre-existing schema implemented using JPA-annotated entities. Your real database schema might not be accessible while developing, or it is not a first-class citizen in your application (i.e. you follow a Java-first approach). This section explains how you can generate jOOQ classes from such a JPA model. Consider this model:

@Table(name = "author")
public class Author {

    int       id;

    @Column(name = "first_name")
    String    firstName;

    @Column(name = "last_name")
    String    lastName;

    @OneToMany(mappedBy = "author")
    Set<Book> books;

    // Getters and setters...

@Table(name = "book")
public class Book {

    public int    id;

    @Column(name = "title")
    public String title;

    public Author author;

    // Getters and setters...

Now, instead of connecting the jOOQ code generator to a database that holds a representation of the above schema, you can use jOOQ's JPADatabase and feed that to the code generator. The JPADatabase uses Hibernate internally, to generate an in-memory H2 database from your entities, and reverse-engineers that again back to jOOQ classes.

The easiest way forward is to use Maven in order to include the jooq-meta-extensions library, or jooq-meta-extensions-hibernate starting from jOOQ 3.14 (which then includes the H2 and Hibernate dependencies)


    <!-- The artifactId has been renamed to jooq-meta-extensions-hibernate in jOOQ 3.14 -->

With that dependency in place, you can now specify the JPADatabase in your code generator configuration:

XML (standalone and maven)
<configuration xmlns="">
        <!-- A comma separated list of Java packages, that contain your entities -->
new org.jooq.util.jaxb.Configuration()
  .withGenerator(new Generator()
    .withDatabase(new Database()

        // A comma separated list of Java packages, that contain your entities
        new Property()
myConfigurationName(sourceSets.main) {
  generator {
    database {
      name = 'org.jooq.util.jpa.JPADatabase'
      properties {

        // A comma separated list of Java packages, that contain your entities
        property {
          key = 'packages'
          value = 'com.example.entities'

The above will generate all jOOQ artefacts for your AUTHOR and BOOK tables.

How to organise your dependencies

The JPADatabase will use Spring to look up your annotated entities from the classpath. This means that you have to create several modules with a dependency graph that looks like this:

                        | Your JPA entities |
                             ^         ^
                  depends on |         | depends on
                             |         |
          +---------------------+   +---------------------+
          | jOOQ codegen plugin |   | Your application    |
          +---------------------+   +---------------------+
                             |         |
                   generates |         | depends on
                             v         v
                     | jOOQ generated classes  |

You cannot put your JPA entities in the same module as the one that runs the jOOQ code generator.


Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo