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
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. -
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. -
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);
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!