generate enum class from table with JOOQ

Philip :

I have the following table called YNM:

 id  name
 1   YES
 2   NO 
 3   MAYBE

and want JOOQ to generate the following java enum:

public enum YNM {
   YES,NO,MAYBE;
}

I understand that support for this was dropped in JOOQ 3 for being overly complicated/anti-intuitive. Is there a way of achieving this?

Thanks in advance.

Lukas Eder :

Sure, you could re-implement the removed feature on your side in a few steps:

1. Implement the generator for that enum

You would need to override the JavaGenerator to implement the code generation for the translation of your master data (might be several tables) to enums. How that works is entirely up to you, e.g. you could have:

  • Single column master data tables
  • ID/VALUE mapping tables
  • ID/VALUE/Comment mapping tables
  • Other layouts

2. Generate ForcedType configurations for those enums

Whenever such a master data table is referenced, you should re-wire the foreign key column to that enum using a <forcedType/> configuration. This is best done by configuring your code generation programmatically, as that would allow you to have more dynamic control over your jOOQ code generation configuration.

This step is documented more in detail in Bill O'Neil's answer.

3. Prevent the generation of the master data tables

In addition to the above, you should probably remove the master data tables themselves from your generated output. In your case, this should result in the configuration:

<excludes>YNM</excludes>

Or, if you have more than one master data table:

<excludes>YNM|OTHER_MASTER_DATA_TABLE|...</excludes>

Excluding these tables will prevent accessing them from jOOQ client code, as well as remove foreign key information in generated code, which might be confusing.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=444444&siteId=1