0

The following mapper configuration works fine (MyPojo has no other constructor)

class MyPojo {
    private Long id;
    private String name;

    // No constructor
}
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);

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;
    }
}

This causes the mapper to not work. 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 argument matching each of the column in the SELECT clause. I don't want to add overloaded constructors (the SELECT column list is huge).

How do set up the mapper to invoke the setters as opposed to a constructor?

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
Keerthi
  • 466
  • 6
  • 12

1 Answers1

0

A SimpleFlatMapper solution

This answer does not help you achieve this with SimpleFlatMapper (which I don't know well enough), but as future visitors of this question might be interested in an out-of-the-box jOOQ solution, I'm answering anyway.

An out-of-the-box jOOQ solution

As of jOOQ 3.14, there isn't a simple way to prevent jOOQ's DefaultRecordMapper from using calling one of the constructors rather than assigning values to the properties directly. Starting from a future jOOQ version, this will be possible: https://github.com/jOOQ/jOOQ/issues/10349

Since you're already aliasing all columns of your query, you could add one constructor that is annotated with @ConstructorProperties and the exact set of names from your query:

class MyPojo {
    private Long id;
    private String name;

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

    @ConstructorProperties({ "id", "name" })
    public MyPojo(Long id, String name) {
        this.id = id;
        this.name = name;
    }
}

I know you said you didn't want to add more constructors, but maybe, you can generate this one with your IDE?

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509