Available in versions: Dev (3.20) | Latest (3.19) | 3.18 | 3.17 | 3.16 | 3.15 | 3.14

KotlinGenerator

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

jOOQ can generate Kotlin code instead of Java code, which allows for leveraging a few kotlin language features also in generated code.

In order to use the KotlinGenerator, simply place the following class reference into your code generation configuration:

XML (standalone and maven)
Programmatic
Gradle (Kotlin)
Gradle (Groovy)
Gradle (third party)
<configuration>
  <generator>
    <name>org.jooq.codegen.KotlinGenerator</name>
  </generator>
</configuration>

See the configuration XSD, standalone code generation, and maven code generation for more details.

new org.jooq.meta.jaxb.Configuration()
  .withGenerator(new Generator()
    .withName("org.jooq.codegen.KotlinGenerator")
  )

See the configuration XSD and programmatic code generation for more details.

// The jOOQ-codegen-gradle plugin has been introduced in version 3.19 only.
// The jOOQ-codegen-gradle plugin has been introduced in version 3.19 only.
generationTool {
  generator {
    name = "org.jooq.codegen.KotlinGenerator"
  }
}

See the configuration XSD and gradle code generation for more details.

Most of the code generation configuration remains the same for as long as it's independent of the generation language. But there are a few kotlin specific configuration flags, which are documented below:

XML (standalone and maven)
Programmatic
Gradle (Kotlin)
Gradle (Groovy)
Gradle (third party)
<configuration>
  <generator>
    <generate>

      <!-- Tell the KotlinGenerator to generate properties in addition to methods for these paths. Default is true. -->
      <implicitJoinPathsAsKotlinProperties>true</implicitJoinPathsAsKotlinProperties>
      <!--  Workaround for Kotlin generating setX() setters instead of setIsX() in byte code for mutable properties called
            <code>isX</code>. Default is true. -->
      <kotlinSetterJvmNameAnnotationsOnIsPrefix>true</kotlinSetterJvmNameAnnotationsOnIsPrefix>
      <!-- Generate POJOs as data classes, when using the KotlinGenerator. Default is true. -->
      <pojosAsKotlinDataClasses>true</pojosAsKotlinDataClasses>

      <!-- Generate non-nullable types on POJO attributes, where column is not null. Default is false. -->
      <kotlinNotNullPojoAttributes>false</kotlinNotNullPojoAttributes>

      <!-- Generate non-nullable types on Record attributes, where column is not null. Default is false. -->
      <kotlinNotNullRecordAttributes>false</kotlinNotNullRecordAttributes>

      <!-- Generate non-nullable types on interface attributes, where column is not null. Default is false. -->
      <kotlinNotNullInterfaceAttributes>false</kotlinNotNullInterfaceAttributes>
      
    </generate>
  </generator>
</configuration>

See the configuration XSD, standalone code generation, and maven code generation for more details.

new org.jooq.meta.jaxb.Configuration()
  .withGenerator(
    new Generate()

      // Tell the KotlinGenerator to generate properties in addition to methods for these paths. Default is true.
      .withImplicitJoinPathsAsKotlinProperties(true)

      // Workaround for Kotlin generating setX() setters instead of setIsX() in byte code for mutable properties called
      // <code>isX</code>. Default is true.
      .withKotlinSetterJvmNameAnnotationsOnIsPrefix(true)

      // Generate POJOs as data classes, when using the KotlinGenerator. Default is true.
      .withPojosAsKotlinDataClasses(true)

      // Generate non-nullable types on POJO attributes, where column is not null. Default is false.
      .withKotlinNotNullPojoAttributes(false)

      // Generate non-nullable types on Record attributes, where column is not null. Default is false.
      .withKotlinNotNullRecordAttributes(false)

      // Generate non-nullable types on interface attributes, where column is not null. Default is false.
      .withKotlinNotNullInterfaceAttributes(false)
  )

See the configuration XSD and programmatic code generation for more details.

// The jOOQ-codegen-gradle plugin has been introduced in version 3.19 only.
// The jOOQ-codegen-gradle plugin has been introduced in version 3.19 only.
generationTool {
  generator {
    generate {

      // Tell the KotlinGenerator to generate properties in addition to methods for these paths. Default is true.
      implicitJoinPathsAsKotlinProperties = true

      // Workaround for Kotlin generating setX() setters instead of setIsX() in byte code for mutable properties called
      // <code>isX</code>. Default is true.
      kotlinSetterJvmNameAnnotationsOnIsPrefix = true

      // Generate POJOs as data classes, when using the KotlinGenerator. Default is true.
      pojosAsKotlinDataClasses = true

      // Generate non-nullable types on POJO attributes, where column is not null. Default is false.
      kotlinNotNullPojoAttributes = false

      // Generate non-nullable types on Record attributes, where column is not null. Default is false.
      kotlinNotNullRecordAttributes = false

      // Generate non-nullable types on interface attributes, where column is not null. Default is false.
      kotlinNotNullInterfaceAttributes = false
    }
  }
}

See the configuration XSD and gradle code generation for more details.

While it is tempting to use non-nullability guarantees that are derived from your table definitions in generated code, it is important to remember that in SQL, there are numerous reasons why an expression that is based on a column declared as SQL NOT NULL can become NULL through the use of SQL operators, such as LEFT JOIN or UNION, etc. In particular, a column with a DEFAULT or IDENTITY expression is "nullable on write", so jOOQ will generate it as nullable despite a NOT NULL constraint!

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo