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 to true): Whether to use the names declared by the java.beans.ConstructorProperties annotation.
  • 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.
  • 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);

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