Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOOQ: how do I add an interface to a generated Record Class

I am generating a set of JOOQ records from a schema using JOOQ 3.6.4 with Java 8.

Some of the tables are reference data that are similarly structured, let's say they have ID, CODE and VALUE columns (they might have other columns, but they all have at least these columns).

In my code, not generated by JOOQ, I have an interface "ReferenceData" that defines accessors that match the code that JOOQ generates for these three columns. I want to tell JOOQ to add an "implements ReferenceData" clause to the Record objects it generates (the code that JOOQ already generates will automatically implement the interfaces).

I'm not asking that JOOQ automatically figure out the interfaces, I'm fine with listing what interfaces each table should implement in the XML configuration.

Question 1: is there any way to configure JOOQ to generate an implements clause without writing a custom generator class?

If I have to write a custom generator class - I still want the definition of what table records implements what interfaces to be in the XML config.

Question 2: Is there an example somewhere of defining custom data in the XML that is communicated down into the custom generator class?

like image 862
Shorn Avatar asked Oct 07 '15 23:10

Shorn


People also ask

How do I generate Java classes for a jOOQ database?

To generate Java classes for our database tables, we'll need the following jooq-config.xml file: The custom configuration requires changes in the <jdbc> section where we place the database credentials and in the <target> section in which we configure the package name and location directory for classes that we'll generate.

How does source code generation work in jOOQ?

While optional, source code generation is one of jOOQ's main assets if you wish to increase developer productivity. jOOQ's code generator takes your database schema and reverse-engineers it into a set of Java classes modelling tables, records, sequences, POJOs, DAOs, stored procedures, user-defined types and many more.

How do I append a schema name to a jOOQ package?

The destination package of your generated classes (within the destination directory) jOOQ may append the schema name to this package if generating multiple schemas, e.g. org.jooq.your.packagename.schema1 org.jooq.your.packagename.schema2 --> <packageName>org.jooq.your.packagename</packageName> <!--

What is jOOQ library in Java?

This library generates Java classes based on the database tables and lets us create type-safe SQL queries through its fluent API. We'll cover the whole setup, PostgreSQL database connection, and a few examples of CRUD operations. 2. Maven Dependencies For the jOOQ library, we'll need the following three jOOQ dependencies:


1 Answers

This can be done using

  • Generator strategies
  • Matcher strategies (which are built-in, XML-based generator strategies)

Generator strategy

With a generator strategy, you'll implement the following code:

public class MyStrategy extends DefaultGeneratorStrategy {
    @Override
    public List<String> getJavaClassImplements(Definition definition, Mode mode) {
        if (mode == Mode.RECORD && definition.getQualifiedName().matches("some regex")) {
            return Arrays.asList(MyCustomInterface.class.getName());
        }
    }
}

The above can then be hooked into your code generator configuration as such:

<generator>
  <strategy>
    <name>com.example.MyStrategy</name>
  </strategy>
</generator>

Matcher strategy

With a matcher strategy, you'll essentially write:

<generator>
  <strategy>
    <matchers>
      <tables>
        <table>
          <expression>A_REGEX_MATCHING_ALL_RELEVANT_TABLES</expression>
          <recordImplements>com.example.MyCustomInterface</recordImplements>
        </table>
      </tables>
    </matchers>
  </strategy>
</generator>

As you can see, matcher strategies are easier to configure than generator strategy, for simple use-cases like yours.

like image 125
Lukas Eder Avatar answered Oct 05 '22 17:10

Lukas Eder