Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoking setters to populate POJO #750

Open
gitkeerthi opened this issue Dec 16, 2020 · 1 comment
Open

Invoking setters to populate POJO #750

gitkeerthi opened this issue Dec 16, 2020 · 1 comment

Comments

@gitkeerthi
Copy link

gitkeerthi commented Dec 16, 2020

Is there a way to force the mapper to use setters to populate a POJO? When my POJO has overloaded constructors the mappers expects an exact constructor matching the arguments with the columns in the SELECT clause. For instance,

class MyPojo {
    private Long id;
    private String name;

    // No constructor
}

Note that MyPojo has default (no-arg) constructor only.

DynamicJdbcMapper<MyPojo> mapper = JdbcMapperFactory.newInstance().addKeys("id").newMapper(MyPojo.class);
ResultQuery<?> query = dsl
        .select(
                MY_TABLE.ID.as("id"),
                MY_TABLE.NAME.as("name"))
        .from(MY_TABLE).where(MY_TABLE.ID.eq(1));

MyPojo pojo = mapper.stream(query.fetchResultSet()).findFirst().orElse(null);

The above works fine.

But in my case MyPojo is a legacy class and comes with a whole bunch of constructors:

class MyPojo {
    private Long id;
    private String name;

    public MyPojo(Long id) {
        this.id = id;
    }
}

Note the Pojo now has an overloaded constructor. This causes the mapper to not work with the same query. I get the following error:

Could not find eligible property for 'name' on  class com.bla.bla.MyPojo

It is apparent that the mapper is trying to look for a constructor with arguments matching the columns in the SELECT clause. I don't want to add overloaded constructors (the SELECT column list is huge). It'd be nice to force the mapper to call the setters regardless.

@arnaudroger
Copy link
Owner

there is some heuristics to find an injection point. If there is a setter it should be able to use it.
I would need a reproducer to see where the problem is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants