Search code examples
javasqljooq

Is jOOQ forcedTypes a must to use postgres enum in java code?


I have a spring boot app with jooq as DAO.

First, I create a enum type role in postgresql (code)

create type role as enum ('admin', 'user');

create table inventory_membership (
  user_id integer references user_profile (id),
  inventory_id integer references inventory (id),
  role role not null default 'user',
  primary key (user_id, inventory_id)
)

Then, as I want to use the java enum role in my app, I configure my pom.xml with below (code):

<forcedTypes>
    <forcedType>
        <userType>com.ocdexperience.sbjooqflywaypoc.db.enums.Role</userType>
        <enumConverter>true</enumConverter>
        <includeExpression>role</includeExpression>
        <includeTypes>.*</includeTypes>
    </forcedType>
</forcedTypes>

Then I run codegen. Here is the InventoryMembership pojo. Note the Role role field.

public class InventoryMembership implements Serializable {

    private static final long serialVersionUID = 632233638;

    private Integer userId;
    private Integer inventoryId;
    private Role    role;

    ...
}

However, if I run codegen without the <forcedTypes> block in pom.xml, I will get Object role instead, which is not good.

public class InventoryMembership implements Serializable {

    private static final long serialVersionUID = -1258093557;

    private Integer userId;
    private Integer inventoryId;
    private Object  role;

    ...
}

Seems <forceTypes> block is a must. However, in this example, there is no <forceTypes> block in pom.xml, but the generated code uses the generated enum automatically, instead of using Object.

Is that only due to the different jooq version? My project uses 3.12.1 vs the example project uses 3.13. Or is it because something else?

Thank you. My project is ready for up and run in https://github.com/ocdexperience/sbjooqflyway-poc.


Solution

  • jOOQ can generate enum types from PostgreSQL database connections out of the box. The problem is, however, that you're using the org.jooq.meta.extensions.ddl.DDLDatabase, which parses your SQL and translates it to H2 behind the scenes. H2 has had a number of problems related to enum types in recent versions as they have just started supporting them. Currently, it is still not possible for jOOQ and H2 to produce the enum type in H2's dictionary views. A relevant bug is this one here: https://github.com/jOOQ/jOOQ/issues/7917

    If you want to profit from jOOQ's native enum support for PostgreSQL, I suggest conencting to an actual PostgreSQL database instead, for source code generation.