Creating jOOQ classes from pure Java

Is there a way to generate jOOQ classes from pure Java code? If not, what is the closest alternative? Ideally I would like to do this in a gradle build.

I found this answer which links to this blog post . The essence of this article is as follows:

  • Start with the JPA model.
  • Convert it to DDL script file (.sql, full of instructions CREATE

    )
  • Create a fresh HSQLDB file and populate it with tables by running a DDL script on it. Save the resulting DB to disk.
  • Load this DB from disk and run jOOQ code on it
  • use the resulting generated jOOQ classes.

There are three things that bother me about this approach:

  • This blog post does everything in Maven (& Ant). I would like to use Gradle, but I can get around this.
  • I have to start with JPA. I would rather start with a clean jOOQ definition of my schema. Does jOOQ have a DSL / API schema definition?
  • No intermediate steps (DDL script and HSQLDB creation) are needed as a final product. Instead, they are only needed to transform the JPA model into something that jOOQ understands as input. There should be a cleaner (Java only) way even if jOOQ doesn't have its own schema definition API. There is one?
+3


source to share


2 answers


Jooq needs some tables that must be created before starting. You can use Flyway for this (and you should use migrations of it too).

Once you have all your tables, you can use this snippet to have Jooq generate source files for your tables:

import org.jooq.util.GenerationTool;
import org.jooq.util.jaxb.*;

Configuration configuration = new Configuration()
    .withJdbc(new Jdbc()
        .withDriver("org.postgresql.Driver")
        .withUrl("jdbc:postgresql://localhost/your_database")
        .withUser("username")
        .withPassword("password"))
    .withGenerator(new Generator()
        .withName("org.jooq.util.DefaultGenerator")
        .withDatabase(new Database()
                .withName("org.jooq.util.postgres.PostgresDatabase")
                .withIncludes(".*")
                .withSchemata(new Schema().withInputSchema("your_schema"))
        )
        .withTarget(new Target()
            .withPackageName("jooq.generate")
            .withDirectory("src/main/java")));
try {
  GenerationTool.generate(configuration);
} catch (Exception e) {
  e.printStackTrace();
}

      

This will leave some .java

files on src/main/java path

. Customize snippet with database settings, etc.

This is similar to how we do something in our projects at work. Jooq plays very well with Flyway . You can check the Jooq documentation about code generation .

There is a maven plugin that we are not using because we have a multiple tenant setup that requires some runtime configuration when migrating and generating code.



My guess is that once you've prepared your Jooq generation in a static main method on some class, you can run it from a gradle task. I can only point you to the Jooq documentation for running it from gradle here

Edit : After studying curiosity, I go through the documentation and my own generated code, I see that you can extend the classes Record

and TableImpl<Record>

to create your hand-crafted schema classes.

You can see here a small snippet of the important parts in the Record

and classes Table

.

Anyway, please note that Jooq (and Flyway) don't want you to avoid SQL, but embrace it. The usual way to do this is

  • you create database objects using SQL scripts
  • you are using Jooq generator to create schema classes
  • you are using them in the Jooq DSL context.
+3


source


Building on @ggalmazor's answer , I ended up using mostly Java code with a minimal core of SQL scripting. My code generation process now consists of three simple steps:

  • Write SQL DDL scripts (i.e. CREATE TABLE

    )
  • Launch a flyover, create a database with internal memory.
  • Run jOOQ on the same database to generate Java code.

Both Flyway and jOOQ are fully configured in Java code; no xml config files.



This is my code:

public class Main {

    public static void main(String... args) {
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
        ds.setUser("u");
        ds.setPassword("p");

        // Flyway
        Flyway flyway = new Flyway();
        flyway.setDataSource(ds);
        flyway.setLocations("classpath:com.example.datadefinition.flyway.migrations");
        flyway.migrate();

        // jOOQ
        try (Connection c = ds.getConnection()) {
            Configuration configuration = new Configuration()
                .withGenerator(new Generator()
                    .withName(MyOwnGenerator.class.getCanonicalName())
                    .withDatabase(new Database()
                        .withName(H2Database.class.getCanonicalName())
                        .withIncludes(".*")
                        .withExcludes("")
                        .withInputSchema("PUBLIC")
                    )
                    .withTarget(new Target()
                        .withPackageName("com.example.lib.data")
                        // jOOQ will create package folders for com.example.lib.data
                        .withDirectory("../sibling-project/src/main")
                    )
                );

            GenerationTool tool = new GenerationTool();
            tool.setConnection(c);
            tool.run(configuration);
        } catch (SQLException e) {
            // sql connection problems
            e.printStackTrace();
        } catch (Exception e) {
            // run failed
            e.printStackTrace();
        }
    }
}

public class MyOwnGenerator extends JavaGenerator {

    public SwsGenerator() {
        setStrategy(new MyOwnGeneratorStrategy());
    }

}

public class MyOwnGeneratorStrategy extends DefaultGeneratorStrategy {

}

      

+2


source







All Articles