Skip to content

Dynamically select mapped class constructor in SimplePropertyRowMapper #35529

@hansdesmet

Description

@hansdesmet

When I have a class with multiple constructors:

class Person {
  private int id;
  private String name;
  Person(int id, String name) {
    this.id = id;
    this.name = name;
  }
  Person(String name) {
    this(0, name);
  }
}

I can't use it as a parameter of the method query of JdbcClient:

jdbcClient.sql("select id,name from persons").query(Person.class).list();

It causes an exception:

java.lang.IllegalStateException: No primary or single unique constructor found

This is a pity: when i make a new Person somewhere in my code, i can't type:

new Person("John");

but I must type (id is an autonumber in the database):

new Person(0, "John"); // a surrogate value for id

Is it possible to relax this one-constructor restriction a bit?

  • If JdbcClient detects multiple constructors, it counts the number of columns in the ResultSet of the query.
  • If there is only one constructor with the same number of parameters as this number of columns, it uses that constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: dataIssues in data modules (jdbc, orm, oxm, tx)status: waiting-for-triageAn issue we've not yet triaged or decided on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions