JOOQ: how to add an interface to the created record class

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

Some of the tables are referenced data that are similarly structured, say that they have ID, CODE, and VALUE columns (they can have other columns, but they all have at least these columns).

In my non-JOOQ code, I have a "ReferenceData" interface that defines accessors that correspond to the code that JOOQ generates for these three columns. I want to tell JOOQ to add the "implements ReferenceData" sentence to the recording objects that it generates (the code that JOOQ already generates will automatically implement the interfaces).

I do not ask JOOQ to automatically detect interfaces, I am fine with listing which interfaces each table should implement in the XML configuration.

Question 1 : is there a way to configure JOOQ to generate an implementation proposal without writing a custom generator class?

If I need to write a custom generator class, I still want to determine which table entries implement which interfaces are in the XML configuration.

Question 2 : Is there an example somewhere to define user data in XML, which is passed to the user generator class?

+6
source share
1 answer

This can be done using

Generator strategy

Using the generator strategy, you 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 be connected to your code generator configuration as such:

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

Pairing strategy

Using the pairing strategy, you 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, matching strategies are easier to configure than generator strategies for simple use cases like yours.

+2
source

All Articles