Home > Blockchain >  Adding properties to enums in JOOQ
Adding properties to enums in JOOQ

Time:12-14

We have been moving away from hibernate to jooq for months now. Pattern we would often do in Hibernate would be writing a custom enum like this...

public enum HBMCapacityType {

    Accepting("Accepting until end of day", true),
    Limited("Limited until end of day", true),
    AtCapacity("At Capacity until further notice",false);

    private final String description;
    private final boolean userOverridable;

    HBMCapacityType(String description, boolean userOverridable) {
        this.description = description;
        this.userOverridable = userOverridable;
    }

    public String getDescription() {
        return this.description;
    }

    public boolean isUserOverridable() {
        return this.userOverridable;
    }
}

Then we are able to use this a column in our hibernate domain objects

@Enumerated(EnumType.STRING)
@Type(type = "pg_enum")
@Column(name = "capacity_type", nullable = false, columnDefinition = "capacity_type")
private HBMCapacityType capacityType;

This is convenient as we can call capacityType.getDescription() when rendering to to the GUI. We have this nice 1:1 mapping of enum types to a description we can use in the GUI. Sometimes we have multiple getters like this say one for description and a few booleans which characterize behaviour associated with that enum. The property isUserOverrideable would be an example of this.

Now looking at JOOQ: since this was defined as an enum in postgres JOOQ automatically generates a type like this...

/**
 * This class is generated by jOOQ.
 */
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public enum CapacityType implements EnumType {

    Accepting("Accepting"),

    Limited("Limited"),

    AtCapacity("AtCapacity");

    private final String literal;

    private CapacityType(String literal) {
        this.literal = literal;
    }

    @Override
    public Catalog getCatalog() {
        return getSchema().getCatalog();
    }

    @Override
    public Schema getSchema() {
        return Wastecoordinator.WASTECOORDINATOR;
    }

    @Override
    public String getName() {
        return "capacity_type";
    }

    @Override
    public String getLiteral() {
        return literal;
    }

    /**
     * Lookup a value of this EnumType by its literal
     */
    public static CapacityType lookupLiteral(String literal) {
        return EnumType.lookupLiteral(CapacityType.class, literal);
    }
}

CodePudding user response:

I guess your question is about how to add custom properties to jOOQ generated enums? There are multiple approaches:

Using a custom code section

One way to achieve the same functionality is by adding a custom code section as described here. You'll add a generateEnumClassFooter() method to your own JavaGenerator subclass, and generate the necessary code there. Unlike in your original code, you can't modify the properties of enum values, so you'll just have to switch over this, instead.

Move the logic outside of the enum

You can always just write a static utility of the form

public static boolean isUserOverridable(HBMCapacityType type) {
    return switch (type) { ... };
}

It's less object oriented, but maybe, that doesn't matter all that much?

Keep 2 separate enums

You don't have to use jOOQ's enum. You can translate it to your own hand-written enum and attach an EnumConverter to your generated code to map between the jOOQ enum type and yours.

  • Related