Search code examples
sqlpojojooqmodelmapper

jOOQ Custom Pojo & DAO Generation


Problem

I'm having some issues configuring mapping to custom Pojos during code generation.

Question

I have implemented RecordMapperProvider but wondering how I register it to be used during the code generation phase, or even if that is possible?

More Context

I love the fact that Pojos & DAOs are generated but I want to define the Pojo myself without too much configuration code. I am using ModelMapper to map from Type to Target:

@Override
public <R extends Record, E> RecordMapper<R, E> provide(RecordType<R> recordType,
                                                        Class<? extends E> type) {

    if (mapping.containsKey(type)) {
        return record -> modelMapper.map(mapping.get(type), type);
    }

    return new DefaultRecordMapper<>(recordType, type);
}

If it helps, I am configuring jOOQ using a DefaultConfiguration object (which is a bean):

@Bean
public DefaultConfiguration configuration() {
    DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
    jooqConfiguration.setConnectionProvider(dataSourceConnectionProvider());
    jooqConfiguration.setExecuteListenerProvider(new DefaultExecuteListenerProvider(
            jooqToSpringExceptionTranslator()));
    jooqConfiguration.setSQLDialect(
            SQLDialect.valueOf(env.getRequiredProperty("jooq.sql.dialect")));
    jooqConfiguration.setRecordMapperProvider(new JooqRecordMapperFactory(modelMapper()));

    return jooqConfiguration;
}

And then for Code Generation I am configuring it in gradle:

jooq {
version = '3.10.5'
edition = 'OSS'

myDb(sourceSets.getByName("main")) {
    jdbc {
        driver = dbDriver
        url = dbUrl
        user = dbUsername
    }
    generator {
        name = 'org.jooq.util.JavaGenerator'
        strategy {
            name = 'org.jooq.util.DefaultGeneratorStrategy'
        }
        database {
            name = 'org.jooq.util.postgres.PostgresDatabase'
            inputSchema = dbSchema
        }
        generate {
            relations = true
            deprecated = false
            records = true
            immutablePojos = true
            fluentSetters = true
            daos = true
        }
        target {
            packageName = 'com.textiq.quinn.common.dao.model.generated'
        }
    }
}
}

I am sure there is a disconnect here between both configurations but I can't glean from the documentation how I synch these. Ideally I want jOOQ to generate Pojos (based on the mapping that ModelMapper provides in my implementation of RecordMapperProvider) and also have jOOQ provide the DAO's for these Pojos. Is this possible? The documentation states:

If you're using jOOQ's code generator, you can configure it to generate POJOs for you, but you're not required to use those generated POJOs. You can use your own. See the manual's section about POJOs with custom RecordMappers to see how to modify jOOQ's standard POJO mapping behaviour.

Source: https://www.jooq.org/doc/3.9/manual/sql-execution/fetching/pojos/

Which to me indicates the possibility of this but only leads me to implementing RecordMapperProvider and nothing after that.


Solution

  • I have implemented RecordMapperProvider but wondering how I register it to be used during the code generation phase, or even if that is possible?

    No, it's not possible, out of the box.

    I love the fact that Pojos & DAOs are generated but I want to define the Pojo myself without too much configuration code

    Then, I suggest turning off the generation of POJOs and DAOs and roll your own. Either, create manual implementations of DAOs, or extend the JavaGenerator to do so.