arnaudroger / SimpleFlatMapper

Fast and Easy mapping from database and csv to POJO. A java micro ORM, lightweight alternative to iBatis and Hibernate. Fast Csv Parser and Csv Mapper
http://simpleflatmapper.org
MIT License
435 stars 76 forks source link

Invoking setters to populate POJO #750

Open gitkeerthi opened 3 years ago

gitkeerthi commented 3 years ago

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 commented 3 years ago

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.