spring-projects / spring-projects/spring-framework

Dynamically select mapped class constructor in `SimplePropertyRowMapper`

Open
#35,529 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

in: data status: waiting-for-triage
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at SimplePropertyRowMapper and the JdbcClient query(Person.class) path, then inspect how the ResultSet column count reaches constructor selection. Check existing row-mapper tests for multiple constructors and add coverage for the unique parameter-count match; done means the example mapping works while ambiguous constructors still follow the existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.