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

This documentation is for the unreleased development version of jOOQ. Click on the above version links to get this documentation for a supported version of jOOQ.

Constructor parameter mapping

Supported by ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

When mapping records into immutable classes, the DefaultRecordMapper calls a constructor. If it cannot discover the constructor's parameter names, it has to match record values to constructor parameters by position. A set of settings governs where parameter names are looked up, so that values can be matched by name instead:

  • mapConstructorPropertiesParameterNames (defaults to true): Whether to use the names declared by the java.beans.ConstructorProperties annotation.The annotation is looked up through the org.jooq.ConstructorPropertiesProvider SPI, whose default implementation is in the jOOQ-beans-extensions module.
  • mapRecordComponentParameterNames (defaults to false): Whether to use the component names of a Java java.lang.Record class.
  • mapConstructorParameterNamesInKotlin (defaults to true): Whether to use the parameter names of a Kotlin class's primary constructor, which requires kotlin-reflect on the classpath. This has no effect on Java classes.
  • mapConstructorParameterDefaultsInKotlin (defaults to true): Whether to apply Kotlin constructor parameter default values to parameters that cannot be mapped from any column of the record, instead of null.
  • mapNullToConstructorParameterDefaultsInKotlin (defaults to false): Whether to also apply Kotlin constructor parameter default values to non-nullable parameters whose column value is NULL. Nullable parameters always receive NULL values, as an explicit NULL is semantically different from an absent value. This has no effect if mapConstructorParameterDefaultsInKotlin is turned off.
  • mapConstructorParameterNames (defaults to false): Whether to use parameter names obtained through Java reflection, using Executable.getParameters(). Parameter names are only available if the class was compiled with the javac -parameters flag.

For example, with mapRecordComponentParameterNames turned on, the following projection is mapped by name, even though the order of columns differs from the order of record components:

record BookInfo(String title, int id) {}

List<BookInfo> books = create
    .select(BOOK.ID, BOOK.TITLE)
    .from(BOOK)
    .fetchInto(BookInfo.class);

In Kotlin, parameters with default values may be omitted from the projection:

data class BookInfo(val id: Int, val title: String, val language: String = "en")

// There is no LANGUAGE column in the projection, so the default value "en" is applied
val books = ctx
    .select(BOOK.ID, BOOK.TITLE)
    .from(BOOK)
    .fetchInto(BookInfo::class.java)

Example configuration

Settings settings = new Settings()
    .withMapConstructorParameterNames(true)      // Defaults to false
    .withMapRecordComponentParameterNames(true); // Defaults to false

Feedback

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

The jOOQ Logo