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 totrue): Whether to use the names declared by thejava.beans.ConstructorPropertiesannotation.The annotation is looked up through theorg.jooq.ConstructorPropertiesProviderSPI, whose default implementation is in the jOOQ-beans-extensions module. -
mapRecordComponentParameterNames(defaults tofalse): Whether to use the component names of a Javajava.lang.Recordclass. -
mapConstructorParameterNamesInKotlin(defaults totrue): Whether to use the parameter names of a Kotlin class's primary constructor, which requireskotlin-reflecton the classpath. This has no effect on Java classes. -
mapConstructorParameterDefaultsInKotlin(defaults totrue): Whether to apply Kotlin constructor parameter default values to parameters that cannot be mapped from any column of the record, instead ofnull. -
mapNullToConstructorParameterDefaultsInKotlin(defaults tofalse): Whether to also apply Kotlin constructor parameter default values to non-nullable parameters whose column value isNULL. Nullable parameters always receiveNULLvalues, as an explicitNULLis semantically different from an absent value. This has no effect ifmapConstructorParameterDefaultsInKotlinis turned off. -
mapConstructorParameterNames(defaults tofalse): Whether to use parameter names obtained through Java reflection, usingExecutable.getParameters(). Parameter names are only available if the class was compiled with thejavac -parametersflag.
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!